匿名类型var自动类型推断
using System; namespace Frank { public class Sample { public static void Main(string[] args) { var name = "Bugs Bunny"; var age = 25; var isRabbit = true; Type nameType = name.GetType(); Type ageType = age.GetType(); Type isRabbitType = isRabbit.GetType(); Console.WriteLine("name is type " + nameType.ToString()); Console.WriteLine("age is type " + ageType.ToString()); Console.WriteLine("isRabbit is type " + isRabbitType.ToString()); } } }
输出结果:
name is type System.String
age is type System.Int32
isRabbit is type System.Boolean
使用前必须对匿名类型进行初始化
初始化器不能为空。
一旦声明确定类型后就不能再次改变类型。