c#笔记-基础知识

news/2025/2/27 10:49:03

目录

整数

浮点

decimal

bool

引用

object

string

枚举 关键字 enum

类型转换 

隐式转换

​编辑 显式转换

装箱,拆箱 

常量 

 表达式

​编辑 字符串的使用

比较字符串 

字符格式化

​编辑 截取字符串

字符串的分割

插入字符串 

删除字符串 

字符串的复制

替换字符串 

可变字符串类 

switch

do...while

foreach 

​编辑转移语句

 数组

​编辑 arraylist

属性 

 方法

 传多参

ref 

out 

重载 

结构

类 


整数

浮点

decimal

bool

引用

object

object o1 = 4;
object o2 = "123";

string

左值,右引用

值不影响别的参数,引用影响

           stamp stamp1 = new stamp { name = "1", age = 25 };
            stamp stamp2 = new stamp { name = "2", age = 30 };

            int age = stamp1.age;
            stamp1.age = 35;

            stamp stamp3 = stamp2;
            stamp2.name = "3";
            Console.WriteLine("stamp1 age: "+ stamp1.age);
            Console.WriteLine("age:"+age);
            Console.WriteLine("stamp2 age: " + stamp2.name);
            Console.WriteLine("stamp3 age: " + stamp3.name);

枚举 关键字 enum

 

类型转换 

隐式转换

 显式转换

 

装箱,拆箱 

二者转换过程中必须类型相同

常量 

readonly要定义在类中,可以直接赋值,也可以在构造方法中赋值,const是编译常量在编译时值就已经确定,而readonly是运行时常量,运行时才确定值。

 static void Main(string[] args) //程序入口
 {
     const double PI = 3.14;
     double r;
     Console.WriteLine("shuru");
     r = Convert.ToDouble(Console.ReadLine()); 
//因为readline返回的是字符串,所以要进行转化
     Console.WriteLine(PI*r);
 }

 表达式

使用%运算符时,结果和%左边的符号相同

字符串的使用

比较字符串 

字符格式化

 截取字符串

字符串的分割

插入字符串 

删除字符串 

字符串的复制

 

替换字符串 

可变字符串类 

switch

do...while

foreach 

转移语句

 数组

 arraylist



1) ArrayList list1 = new ArrayList();

2) int[] arr = new int[]{} ;

    ArrayList list2 = new ArrayList(arr) ;

3) ArrayList list3 = new ArrayList(10) ;

ArrayList的方法

1)public virtual int Add(Object value) 

2)  public virtual void Insert(int index, Object value)

3) InsertRange(int Index, 一维数组的名称) 

4) public virtual void  Clear() --移除所有元素

5)public virtual void Remove(Object obhj) ---删除指定的元素

6)public virtual void RemoveAt(int Index) ---删除指定索引的元素

7)public virtual void RemoveRange(int index, int count)--删除指定的多个元素

8) foreach遍历ArrayList

9) IndexOf("A") ---返回A第一次出现的索引的位置,未找到的话返回-1;

10) IndexOf("A", 7) ---从索引7开始查找,未找到返回-1 ;

11) LastIndexOf("A") ---最后一次出现A的索引

12)LastIndexOf("A", 7)---下标0~7这段最后一次出现A的位置索引
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/qq_34177692/article/details/141942547

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//ArrayList类位于System.Collections命名空间内
using System.Collections;
 
//ArrayList是一种动态数组
//ArrayList的容量可以根据需要自动扩充
//ArrayList提供了添加,删除,和插入某一范围元素的方法
//Array List提供了将只读和固定大小包装返回集合的方法,数组不提供
//Array List只能是一维的形式,数组可以多维
 
namespace ArrayListDemo01
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //ArrayList的构造函数
            //以默认(16位)大小来初始化内部的数组
            ArrayList list = new ArrayList();
 
            //用一个ICollection对象来构造,并把集合的元素添加到ArrayList中
            int[] arr1 = {10,9,8,7,6,5,4,3,2,1,0,11,12,13,14,15,16};
            ArrayList list1 = new ArrayList(arr1 );
 
            //用指定大小初始化内部数组
            ArrayList list2 = new ArrayList(20);
 
 
            
 
            Console.WriteLine("list中包含的元素数为:");
            //ArrayList的属性Capacity获取或设置ArrayList中可包含的的元素数
            Console.WriteLine(list.Capacity);
            list.Capacity = 10;
            Console.WriteLine("list可以包含的元素数:");
            Console.WriteLine(list.Capacity);
            
 
            //ArrayList中的属性Count是获取实际包含的元素数
            //如果ArrayList没有被初始化,那么count=0;
            Console.WriteLine("list中实际包含的元素数:");
            Console.WriteLine(list.Count);
 
 
            //ArrayList的初始化
            for(int i=0; i<list.Capacity; i++)
            {
                list.Add(i);
            }
            Console.WriteLine("初始化后,list内包含的元素数:");
            Console.WriteLine(list.Count);
 
            //常用的属性及说明
            //IsFixedSize
            Console.WriteLine(list1.IsFixedSize);  //false
            //IsReadOnly
            Console.WriteLine(list1.IsReadOnly); //false 
 
            //Item 获取或设置指定索引处的元素
            //和数组访问元素的方法一样
            Console.WriteLine(list1[5]);
 
            //ArrayList中的方法
            //public virtual int Add(Object value)
            //virtual方法在派生类中可以不重写,有方法体
            //抽象方法只有声明没有方法体,派生类中必须重写
            list1.Add(17);
            list1.Add(18);
            foreach(int i in list1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
 
            //Insert(), 插入元素
            //public virtual void Insert(int index, Object value)
            list1.Insert(0, 100);
            foreach (int i in list1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
 
            //ArrayList插入一个数组
            //InsertRange()
            int[] arr2 = {11,22,33,44,55,66,77 };
            list1.InsertRange(5,arr2);
            foreach (int i in list1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
 
            //ArrayList删除一个元素
 
            int[] array = {11,2,3,44,55,55,55,66,77,88,99,100,101,102 };
            ArrayList al1 = new ArrayList(array);
            //Remove()从ArrayList中移除特定对象的第一个匹配项
            al1.Remove(55);//括号里面是ArrayList中的元素
            foreach (int i in al1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
 
            //RemoveAt()移除指定索引的元素
            al1.RemoveAt(5);//括号里面是索引
            foreach (int i in al1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
 
            //RemoveRange()
            //public virtual void RemoveRange(int index, int count)
            al1.RemoveRange(4, 3);
            foreach (int i in al1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
 
            //clear()删除所有元素
            al1.Clear();
            Console.WriteLine(al1.Count);//获取实际包含的元素数
            Console.WriteLine(al1.Capacity);//获取或设置可包含的元素数
            Console.WriteLine();
 
            int[] al2 = new int[] { 11, 33, 2, 5, 10, 10, 10, 15, 63, 64, 54 };
            ArrayList arrList = new ArrayList(al2);
            //Indexof("A")返回第一次出现的索引位置,未找到返回-1
            Console.WriteLine("第一次出现10的索引位置为:{0} ", arrList.IndexOf(10));
 
            //IndexOf("A", 7)从索引7开始查找,未找到返回-1,包含7这个索引位置
            Console.WriteLine(arrList.IndexOf(10, 5));//=5;
            //LastIndexOf(10)从后面开始检索10第一次出现的索引
            Console.WriteLine(arrList.LastIndexOf(10));//=6从
            Console.WriteLine(arrList.LastIndexOf(10, 4));//=4
 
            //public virtual bool Contains(Object item)
            Console.WriteLine(arrList.Contains(20));//=false
            Console.WriteLine(arrList.Contains(10));//=true
        }
    }
}

属性 

 

 

 方法

 传多参

ref 

 

out 

 

重载 

结构

类 

 


http://www.niftyadmin.cn/n/5869981.html

相关文章

DevOps全流程

DevOps全流程 面试速记 DevOps 是一种融合开与运维的软件研发实践方法&#xff0c;规划从需求调研到开发&#xff0c;测试&#xff0c;部署等全流程&#xff0c;通过Jenkins 和Kubernetes 等自动化工具集成部署实现从代码编写到部署的无缝衔接&#xff0c;加速软件交付周期。我…

Redis分布式缓存面试题

为什么使用分布式缓存&#xff1f; 1. 提升性能 降低延迟&#xff1a;将数据缓存在离应用更近的地方&#xff0c;减少数据访问时间。减轻数据库压力&#xff1a;缓存频繁访问的数据&#xff0c;减少对后端数据库的请求&#xff0c;提升系统响应速度。 2. 扩展性 水平扩展&a…

计算机网络之传输层(传输层的功能)

一、数据分段与重组 传输层从会话层接收数据&#xff0c;并将其分割成较小的数据段&#xff0c;以适应网络层的最大传输单元&#xff08;MTU&#xff09;限制。在目的端&#xff0c;传输层负责将这些数据段重新组合成原始数据&#xff0c;确保数据的完整性和正确性。 二、端口…

设计模式-行为型-责任链模式

1. 责任链模式概述 责任链模式&#xff08;Chain of Responsibility Pattern&#xff09; 是一种行为型设计模式&#xff0c;它允许多个对象依次处理请求&#xff0c;形成一条处理链。每个对象都包含对下一个对象的引用&#xff0c;如果它无法处理请求&#xff0c;则将请求传递…

支持selenium的chrome driver更新到133.0.6943.141

最近chrome释放新版本&#xff1a;133.0.6943.141 如果运行selenium自动化测试出现以下问题&#xff0c;是需要升级chromedriver才可以解决的。 selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only s…

JAVA-如何理解Mysql的索引

一、索引的概念 索引是一种特殊的文件&#xff0c;包含着对数据表里所有记录的引用(指针/地址)。可以对表中的一列或多列创建索引&#xff0c; 并指定索引的类型&#xff0c;各类索引有各自的数据结构实现。 二、索引是什么&#xff0c;用来干嘛 数据库中的表、数据、索引之间的…

IO 和NIO有什么区别?

IO 与 NIO 的区别详解 Java 中的 IO&#xff08;Input/Output&#xff09; 和 NIO&#xff08;New IO 或 Non-blocking IO&#xff09; 是两种不同的输入输出处理机制&#xff0c;主要区别体现在设计模型、性能优化和应用场景上。以下是详细对比&#xff1a; 1. 阻塞与非阻塞模…

【C++笔记】C++11智能指针的使用及其原理

【C笔记】C11智能指针的使用及其原理 &#x1f525;个人主页&#xff1a;大白的编程日记 &#x1f525;专栏&#xff1a;C笔记 文章目录 【C笔记】C11智能指针的使用及其原理前言1.智能指针的使用场景分析2. RAII和智能指针的设计思路3. C标准库智能指针的使用4. 智能指针的原…