作用
匿名类型常作为LINQ的查询结果
语法
- new{字段=值}//字段不需要类型 编辑器根据值推断类型
var student=new {Name=“LINQ”,Age=22};
print(student.Name); - new{其他已赋值的变量}
int age=22;
var student=new{age};
print(student.age) - new{成员访问表达式}
public class Other
{
public string Name=“LINQ”;
}
var student=new{ Other.Name};
print(student.Name);
三种形式可以相互搭配
注意
匿名类型的成员是只读类型属性
匿名类型只能作为局部变量,不能作为类成员
匿名类型没有变量类型的名称 所以使用var代替