2. 变量在声明时候,必须被初始化,因为编译器要根据变量的赋值来推断类型,如果未被初始化,编译器也就无法完成推断了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 隐式类型 { class Program { public static int age = 12; static void Main(string[] args) { //被声明的变量是一个局部变量,不能为字段(包括:静态字段 和 实例字段) var i = 100; //变量声明要初始化 var arr = new int[100];//正确可以使用 var str = null; //错误 1 无法将“<null>”赋值给隐式类型的局部变量 //不能使用一个正在声明的变量来初始化隐式类型,例如不能使用下面这样的代码 string s; var stringvariable = s;//错误 2 使用了未赋值的局部变量“s” //不能使用var 来声明方法中的参数类型 } } }