首页 / Unity3d / C#

读一读

数组  都继承于System.Array

int[] numbers = new int[5];
string[,] names = new string[5,4];
byte[][] scores = new byte[5][];
scores[1] = new byte[10];//数组的数组
//[,]是规则数组,每行的列元素都是相同的,变现为矩阵
//[][]是不规则矩阵,每行的列数都是可以不一样的


初始化

int[] numbers2 = new int[]{1,2,3,4,5};
int numbers3 = {1,2,3,4,5};
string[,] names = {{"g","k"},{"h","j"}};

for(int i=0;i<100;i++){}
//初始化,条件,步长
//break  跳出循环
//continue 跳出本次循环,进行下一次


List<int> listInt=new List<int>(){1,2,3};
foreach(var intInList in listInt){}


while(条件){}  //可能一次都不执行
do{} while(条件)  //最少执行一次

#region 名字
//代码块
#endregion

方便管理代码


if(条件){} //条件为true时执行代码块
else{}  //条件为false时执行
//  !反   &&且  ||或者

可为空值类型

int? iNullable = 100; //等同于
System.Nullable<int> iNullable2 = 100;


双??操作符

int ii = iNullable??500;

如果iNullable为空,则赋值为500,不为空赋值为iNullable


装箱:将值类型变为引用类型

引用放在栈上,值放在堆上

int i=100;
object iBoxed = i;


拆箱:将引用类型转换为值类型

int UnBoxing=(int)iBoxed;

C#是强类型,编译的时候就会进行类型检查。


隐式转换类型  

int i=10;  long c=i;
class c1{} 
class c2:c1{}  
c1 c11 = new c2();

小的数据范围到大的数据范围


显示转换(大范围到小范围,会丢失)

double d=10.05;  
int i=(int)d;
c11 is c1;//is判断类型
c11 as c2;//(不能显示转化是不会报错,赋值为null),as转换类型


转换string到int

Covert.ToInt32("100");
Int32.Parse("100");
bool succeed = Int32.Parse("100",out i);

interface ISuper{
    int GetAge();
}

class HePi : ISuper{
    public int GetAge(){
        return 18;
    }
}

接口里面的都是抽象方法,继承接口后,需要实现接口里面的所有方法


abstract class Man{
    puclic abstract int GetAge();
}

class HaPi : Man{
        public int GetAge(){
            return 18;
        }
}

继承这个抽象类,必须实现里面的抽象方法,抽象类里面可以有不抽象的方法,像平常类那样


class People{
    public People(..参数){//构造函数}
    private int age;
    public int getAge(){return age;}
}


静态方法

public static int GeyAge(){}   
类名::GetAge()

属性

public int Age{get;set;}  //get和set也是方法可以自定义
public string Name{get{return...}};