5

我最近在 C# 中遇到了一些相当莫名其妙的事情。在我们的代码库中,我们有一个TreeNode类。在更改某些代码时,我发现无法将变量分配给Nodes属性。经过仔细检查,很明显该属性是只读的,并且这种行为是可以预料的。

奇怪的是,我们的代码库在此之前一直依赖于为Nodes属性分配一些匿名类型,并且编译和工作得很好。

总结一下:为什么这项任务AddSomeNodes首先会起作用?

using System.Collections.Generic;

namespace ReadOnlyProperty
{
    public class TreeNode
    {
        private readonly IList<TreeNode> _nodes = new List<TreeNode>();

        public IList<TreeNode> Nodes
        {
            get { return _nodes;  }
        }
    }

    public class TreeBuilder
    {
        public IEnumerable<TreeNode> AddSomeNodes()
        {
            yield return new TreeNode
             {
                Nodes = { new TreeNode() }
             };
        }

        public IEnumerable<TreeNode> AddSomeOtherNodes()
        {
            var someNodes = new List<TreeNode>();

            yield return new TreeNode
             {
                Nodes = someNodes
             };
        }
    }
}
4

5 回答 5

4

AddSomeNodes is not creating an instance of List<TreeNode> because that syntax is a collection initializer (therefore it is not assigning to Nodes meaning it doesn't break the readonly contract), the compiler actually translates the collection initializer into calls to .Add.

The AddSomeOtherNodes call actually tries to re-assign the value, but it is readonly. This is also the object initializer syntax, which translates into simple property calls. This property does not have a setter, so that call generates a compiler error. Attempting to add a setter that sets the readonly value will generate another compiler error because it is marked readonly.

From MSDN:

By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.

Also, just to clarify, there are no anonymous types in play in your code - it is all initializer syntax.


Unrelated to your question, but in the same area.

Interestingly, the Nodes = { new TreeNode() } syntax doesn't work with a local member, it only seems to work when it is nested inside an object initializer or during object assignment:

List<int> numbers = { 1, 2, 3, 4 }; // This isn't valid.
List<int> numbers = new List<int> { 1, 2, 3, 4 }; // Valid.

// This is valid, but will NullReferenceException on Numbers
// if NumberContainer doesn't "new" the list internally.
var container = new NumberContainer()  
{
    Numbers = { 1, 2, 3, 4 }
};

The MSDN documentation doesn't seem to have any clarification on this.

于 2012-07-10T14:15:50.047 回答
2

Your nodes property is not being assigned.

Using the special collection initializer:

CollectionProperty = { a, b, c };

Is changed to:

CollectionProperty.Add(a);
CollectionProperty.Add(b);
CollectionProperty.Add(c);
于 2012-07-10T14:16:41.157 回答
1

这不是分配,而是向ICollection

 Nodes = {new TreeNode() }, that is why it works.
于 2012-07-10T14:15:46.590 回答
1

I compiled your code (after removing the AddSomeOtherNodes method) and opened it in Reflector and here is the result:

public IEnumerable<TreeNode> AddSomeNodes()
{
    TreeNode iteratorVariable0 = new TreeNode();
    iteratorVariable0.Nodes.Add(new TreeNode());
    yield return iteratorVariable0;
}

As you can see, with this syntax the Add method is called on the Nodes variable.

于 2012-07-10T14:19:20.510 回答
0

The compiler equivalent to what happened is this:

public IEnumerable<TreeNode> AddSomeNodes()
{
    TreeNode node = new TreeNode();
    node.Nodes.Add(new TreeNode());

    yield return node;
}

The important distinction here is that they used the collection initializer syntax to assign the value.

于 2012-07-10T14:18:34.007 回答