C# · 12月 29, 2021

在初始化中得到并设置误解:Jeffrey Richter,CLR通过C#

我刚刚在Jeffrey Richter的书(CLR通过C#4.0,第257页)发现我的代码,并误解了为什么它的工作原理. 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.