ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## **变量声明** 变量类型 变量名1,变量名2, ``` int a,b; ``` ## **var(自动类型) 后面可以跟任何变量类型的值,编译器会自动识别变量类型** ``` var x=3;//等同 int x=3 //Console.WriteLine(x.getType().name);//int32 var x=3L;;//等同 long x=3 //Console.WriteLine(x.getType().name);//int64 var x=3.0;//等同 float x=3.0 Console.WriteLine(x.getType().name);//Double var x=3.0F; Console.WriteLine(x.getType().name);//Single 即float var x="hello"; Console.WriteLine(x.getType().name);//string ``` ## **变量赋值** ``` int a,b; a=1; b=2; ``` ## **静态变量(只能在类里)** ``` class Test{ int a,b;//实例变量 static int c=3;//静态变量(类变量) } ``` ## **局部变量(即类方法中定义的变量)** ``` class Test { static int c = 3; static void Main(string[] args) { for (int i = 0; i <=20; i++) { Console.WriteLine(i.ToString()); } } } ``` ## **全局代码(文件中直接写表达式)** >[info]全局代码示意 ``` Console.WriteLine("Hello, World!"); string name = "马云"; Console.WriteLine(name); //注意上面直接写了表达式,则为全局代码,下面定义的类会被直接忽视不会执行 class Test { static int c = 3; static void Main(string[] args) { for (int i = 0; i <=20; i++) { Console.WriteLine(i.ToString()); } } } ```