有什么区别吗
var list = new List<UserType>
{
new UserType(...),
new UserType(...),
};
和
var list = new List<UserType>()
{
new UserType(...),
new UserType(...),
};
?
我过去总是使用第二个,认为我只需要调用列表的无参数(或任何其他)构造函数...
最佳答案
是一样的。来自 MSDN :
The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax)
同样的规则适用于列表和普通对象初始值设定项:
var foo = new Bar {
Prop = "value"
};
关于c# - 从初始化器创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21383499/