匿名类型:
*提供了一种方便的方法,用来将一组只读属性封装到单个对象中,而无需首先显示定义一个类型
*类型名由编译器生成
*结合new运算符和对象初始值设定项创建匿名类型
*匿名类型是class类型,直接派生自object
*如下示例,查找年龄是两岁的猫
public class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } public Cat() { } public Cat(string name) { this.Name = name; } }
static void Main(string[] args) { List<Cat> cats = new List<Cat> { new Cat { Name = "Sylvester", Age = 8 }, new Cat { Name = "Whiskers", Age = 2 }, new Cat { Name = "Sasha", Age = 14 } }; var cat = from pet in cats where pet.Age == 2 select pet; Console.WriteLine($"{cat.First().Name}'age is 2 "); //output //Whiskers'age is 2 }
标签:Name,C#,Age,Cat,匿名,类型,new,public From: https://www.cnblogs.com/echo-efun/p/18420779