加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 03:47:00 所属栏目:百科 来源:网络整理
导读:我刚刚在Jeffrey Richter的书(CLR通过C#4.0,第257页)发现我的代码,并误解了为什么它的工作原理. public sealed class Classroom { private ListString m_students = new ListString(); public ListString Students { get { return m_students; } } public Cl
我刚刚在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);
        }
    }

结果:

Jeff
Kristin

正如你所看到的,我们有一个名为“学生”的访问器属性,它只有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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读