我在 C# 中偶然发现了这种奇怪的语法形式,并试图弄清楚它的含义以及如何使用它。网络上似乎没有关于此的任何文档。
object data = new { var1 = someValue, var2 = anotherValue }
此语法没有错误,所以我认为这是 C# 允许的。这是什么意思?
最佳答案
这是一个匿名类型。它基本上会像一个类一样工作:
class anonymous
{
public readonly type var1; // "type" is the type of somevalue
public readonly type var2; // "type" is the type of anothervalue
}
实例化为
var data = new anonymous { var1 = somevalue, var2 = anothervalue };
关于c# - 这个语法是什么意思?新的 { },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17624878/