只读集合属性的C#对象初始化 [英] C# object initialization of read only collection properties

查看:134
本文介绍了只读集合属性的C#对象初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的生活,我无法弄清楚什么是在实例篇C#code以下的事情。测试类的集合(列表)属性为只读设置了,但我似乎可以分配给它的对象初始化。

**编辑:修正了列表消气的问题。

 使用系统;
使用System.Collections.Generic;
使用NUnit.Framework;命名空间WF4.UnitTest
{
    公共类MyClass的
    {
        私人列表<串GT; _strCol =新的List<串GT; {测试1};        公开名单<串GT; StringCollection
        {
            得到
            {
                返回_strCol;
            }
        }
    }    [的TestFixture]
    公共类单元测试
    {
        [测试]
        公共无效MyTest的()
        {
            MyClass的C =新MyClass的
            {
                //吧?此属性为只读!
                StringCollection = {测试2,TEST3}
            };            //没有这些东西编译(因为我不希望他们)
            //c.StringCollection = {测试1,测试2};
            //c.StringCollection =新的收集和LT;串>();            //'TEST1','test2的,TEST3输出
            的foreach(字符串s在c.StringCollection)Console.WriteLine(S);
        }
    }
}


解决方案

  MyClass的C =新MyClass的
{
    StringCollection = {测试2,TEST3}
};

被翻译成这样:

  MyClass的TMP =新MyClass的();
tmp.StringCollection.Add(测试2);
tmp.StringCollection.Add(TEST3);
MyClass的C = tmp目录;

这是从来没有试图调用setter - 它只是调用添加上调用的的getter 的结果。请注意,这也是没有的结算的原始集合无论是。

此进行更详细的在C#4规范的部分7.6.10.3描述

编辑:正如兴趣点,我微微一惊,它调用吸气的两倍。我预期,一旦调用吸气,然后调用添加两次...规格包括这表明一个例子。

For the life of me, I cannot figure out what is going on in the example piece of C# code below. The collection (List) property of the test class is set as read only, but yet I can seemingly assign to it in the object initializer.

** EDIT: Fixed the problem with the List 'getter'

using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace WF4.UnitTest
{
    public class MyClass
    {
        private List<string> _strCol = new List<string> {"test1"};

        public List<string> StringCollection 
        {
            get
            {
                return _strCol;
            }
        }
    }

    [TestFixture]
    public class UnitTests
    {
        [Test]
        public void MyTest()
        {
            MyClass c = new MyClass
            {
                // huh?  this property is read only!
                StringCollection = { "test2", "test3" }
            };

            // none of these things compile (as I wouldn't expect them to)
            //c.StringCollection = { "test1", "test2" };
            //c.StringCollection = new Collection<string>();

            // 'test1', 'test2', 'test3' is output
            foreach (string s in c.StringCollection) Console.WriteLine(s);
        }
    }
}

解决方案

This:

MyClass c = new MyClass
{
    StringCollection = { "test2", "test3" }
};

is translated into this:

MyClass tmp = new MyClass();
tmp.StringCollection.Add("test2");
tmp.StringCollection.Add("test3");
MyClass c = tmp;

It's never trying to call a setter - it's just calling Add on the results of calling the getter. Note that it's also not clearing the original collection either.

This is described in more detail in section 7.6.10.3 of the C# 4 spec.

EDIT: Just as a point of interest, I was slightly surprised that it calls the getter twice. I expected it to call the getter once, and then call Add twice... the spec includes an example which demonstrates that.

这篇关于只读集合属性的C#对象初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆