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

c# – 在对象初始值设定项中使用数组初始值设定项时的NRE

发布时间:2020-12-15 22:14:37 所属栏目:百科 来源:网络整理
导读:参见英文答案 Empty collection initializer for list property results in null????????????????????????????????????1个 在以下情况中使用数组初始值设定项时,我观察到一些奇怪的行为(在VS2013中测试过): class A { public Listint Items { get; set; } }
参见英文答案 > Empty collection initializer for list property results in null????????????????????????????????????1个
在以下情况中使用数组初始值设定项时,我观察到一些奇怪的行为(在VS2013中测试过):

class A
    {
        public List<int> Items { get; set; }
    }

    void Main(string[] args)
    {
        // (1) it compiles,but failing with NRE in runtime
        var a = new A { Items = { 1,2,3 } };

        // (2) it does not compile,as expected
        List<int> b = { 1,3 };
    }

实际上我会期望在情况(1)中出现编译器错误,与我在情况(2)中的情况相同:只能使用数组初始化表达式来分配给数组类型.请尝试使用新表达式.但是case(1)编译时没有任何问题,并且在运行时可以预期NullReferenceException失败.有人可以解释为什么编译器允许这种情况(1)?

解决方法

以下是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.

因为你在对象初始化器中使用了一个集合初始化器,行为是不同的,这段代码:

var a = new A { Items = { 1,3 } };

编译成这样的东西(你可以通过查看IL来验证这一点):

var a = new A();
a.Items.Add(1);
a.Items.Add(2);
a.Items.Add(3);

这当然会抛出NullReferenceException,因为Items为null.

您可以通过向A类添加构造函数来修复崩溃:

class A
{
    public List<int> Items { get; set; }
    public A() { Items = new List<int>(); }
}

(编辑:李大同)

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

    推荐文章
      热点阅读