Code Code publicclass AnonymousTypes
{ //AutomaticProperties(自动属性) publicint ID { get; set; } publicstring Name { get; set; } publicint Age { get; set; }
publicvoid AnonymousTypesTest()
{ //CollectionInitializers(集合初始化器) List<AnonymousTypes> list =new List<AnonymousTypes>
{ //ObjectInitializers(对象初始化器) new AnonymousTypes { ID =1, Name ="webabcd", Age =10 }, new AnonymousTypes { ID =2, Name ="webabcdefg", Age =20 }, new AnonymousTypes { ID =3, Name ="webabcdefghijklmn", Age =30 }
};
// listAnonymousTypes - 匿名类型 var listAnonymousTypes = from l in list where l.Name =="webabcd"
select new { Name = l.Name, Age = l.Age };
foreach (var v in listAnonymousTypes)
{ // v - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense string name = v.Name; int age = v.Age;
}
// 声明匿名类型:将new关键词后面的类型名称省略掉 var person =new { Name ="webabcd", Age =27 }; // person - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense string myName = person.Name; int myAge = person.Age;
}
}