public sealed class Classroom { private List<String> m_students = new List<String>(); public List<String> Students { get { return m_students; } } public Classroom() { } } class Program { static voID Main(string[] args) { Classroom classroom = new Classroom { Students = { "Jeff","Kristin" } }; foreach (var student in classroom.Students) Console.Writeline(student); } }
结果:
JeffKristin
正如你所看到的,我们有一个名为“学生”的访问器属性,它只有getter(不是setter!),而是在’Main’函数中,当我们要初始化’classroom’变量时,我们初始化’Students’字段“课堂”类型:
Classroom classroom = new Classroom { Students = { "Jeff","Kristin" }};
我一直以为,当表达式(int i = 1)的“左侧”中的变量,然后编译器应该访问setter函数,并且在“right-sIDe”(int x = i 2)到getter函数时.
为什么在杰弗里的代码中这么有趣的行为(可能只是对我来说,对不起,如果是这样).
解决方法 从C#5规范的7.6.10.2部分:A member initializer that specifIEs a collection initializer after the equals sign is an initialization of an embedded collection. Instead of assigning a new collection to the fIEld or property,the elements given in the initializer are added to the collection referenced by the fIEld or property. The fIEld or property must be of a collection type that satisfIEs the requirements specifIEd in §7.6.10.3.
所以这段代码:
Classroom classroom = new Classroom { Students = { "Jeff","Kristin" }};
相当于:
Classroom tmp = new Classroom();tmp.Students.Add("Jeff");tmp.Students.Add("Kristin");Classroom classroom = tmp;
基本上,对象初始化器中的=与独立赋值语句不完全相同.
编辑:这段代码
Classroom classroom = new Classroom { Students = new List<string> { "Jeff","Kristin" }};
将无法编译,因为这将尝试为学生调用setter.
总结以上是内存溢出为你收集整理的在初始化中得到并设置误解:Jeffrey Richter,CLR通过C#全部内容,希望文章能够帮你解决在初始化中得到并设置误解:Jeffrey Richter,CLR通过C#所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)