组合列表初始值设定项和对象初始值设定项
是否可以同时组合列表初始值设定项和对象初始值设定项? 给出以下类定义:
class MyList : List<int>
{
public string Text { get; set; }
}
// we can do this
var obj1 = new MyList() { Text="Hello" };
// we can also do that
var obj2 = new MyList() { 1, 2, 3 };
// but this one doesn't compile
//var obj3 = new MyList() { Text="Hello", 1, 2, 3 };
这是设计使然还是只是 c# 编译器的错误或缺失功能?
Is is possible to combine a List initializer and object initializer at the same time?
Given the following class definition:
class MyList : List<int>
{
public string Text { get; set; }
}
// we can do this
var obj1 = new MyList() { Text="Hello" };
// we can also do that
var obj2 = new MyList() { 1, 2, 3 };
// but this one doesn't compile
//var obj3 = new MyList() { Text="Hello", 1, 2, 3 };
Is this by design or is it just a bug or missing feature of the c# compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,查看 C# 规范第 7.6.10 节中的定义,
object-or-collection-initializer
表达式是object-initializer code> 或一个
集合初始化器
。object-initializer
由多个member-initializer
组成,每个成员的形式为initializer =initializer-value
而member-initializer
>collection-initializer由多个element-initializer
组成,每个元素初始化器都是一个non-assigment-expression
。所以它看起来像是设计使然——可能是为了简单起见。老实说,我不能说我曾经想要这样做。 (我通常不会从
List
开始 - 我会编写它。)我真的不想看到:编辑:如果你真的,真的 想要启用此功能,您可以将其放入类中:
此时您可以编写:
No, looking at the definitions from section 7.6.10 of the C# spec, an
object-or-collection-initializer
expression is either anobject-initializer
or acollection-initializer
.An
object-initializer
is composed of multiplemember-initializer
s, each of which is of the forminitializer = initializer-value
whereas acollection-initializer
is composed of multipleelement-initializer
s, each of which is anon-assigment-expression
.So it looks like it's by design - possibly for the sake of simplicity. I can't say I've ever wanted to do this, to be honest. (I usually wouldn't derive from
List<int>
to start with - I'd compose it instead.) I would really hate to see:EDIT: If you really, really want to enable this, you could put this in the class:
at which point you could write:
不,这不是错误。这是语言设计的。
当您编写
此代码时,编译器会有效地将其转换为
当您编写
此代码时,编译器会有效地将其转换为 请
注意,在第一种情况下,您使用的是对象初始值设定项,但在第二种情况下,您使用的是集合初始值设定项。不存在对象和集合初始化器这样的东西。您要么正在初始化对象的属性,要么正在初始化集合。你不能两者兼而有之,这是设计使然。
另外,您不应该从实现集合是一个坏主意吗?
List
派生。请参阅:继承列表No, it's a not a bug. It is by design of the language.
When you write
this is effectively translated by the compiler to
When you write
this is effectively translated by the compiler to
Note that in the first case you are using an object initializer, but in the second case you are using a collection initializer. There is no such thing as an object-and-collection intializer. You are either initializing the properties of your object, or you are initializing the collection. You can not do both, this is by design.
Also, you shouldn't derive from
List<T>
. See: Inheriting List<T> to implement collections a bad idea?如果您想获得类似的功能,请考虑使用构造函数参数:
If you want to get something like this functionality, consider making a constructor argument: