C# 自学笔记

99 阅读1分钟

1.var定义变量之后,变量的类型就不能进行更改啦,因为是在编译的时候确定变量类型,在编译的时候就不会通过。

var value1 = "123str";
value1 = 123; // 此时会报错,无法将int类型隐式转换成string类型

2.数组集合

ushort[] ushorts = new ushort[] { 0, 1, 2};
int[] ints = new int[] { 0, 1 };
Console.WriteLine(ints[0]);

List<string> sttt = new List<string>() { "111", "222" };
Console.WriteLine(sttt[0]);



foreach循环
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars) 
{
  Console.WriteLine(i);
}
// Sort()进行排序
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Array.Sort(cars);
foreach (string i in cars)
{
  Console.WriteLine(i);
}

## System.Linq 命名空间
其他有用的数组方法,如`  Min `, `  Max `, `  Sum `,可以在`System.Linq`命名空间找到:

3.方法

1.static 关键字
  静态方法中调用其他方法要求其他方法也是静态的。ex:static void Main 中调用其他方法,那么这个其他方法需要加static关键字
  或者 通过类的实例化对象进行调用
2.out ref 传值
    // out 传值 res不需要初始赋值
    int res;
    GetSum(1, 3, out res);
    Console.WriteLine(res);
    // ref 传值 res1需要有初始默认值
    int res1 = 0;
    GetRefSum(1, 100, ref res1);
    Console.WriteLine(res1);
    
    static void  GetSum(int a,int b,out int result)
    {
        result = a + b;

    }
    static void GetRefSum(int a, int b, ref int result)
    {
        result = a + b;

    }

4.类

static class Car
静态类中不能声明实例成员
静态类中不能有实例构造函数

5.访问修饰符

方法默认为private 私有的
类默认为 internal 

官网

访问修饰符.png