Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a read-only property with anonymous type

Tags:

c#

.net

I came about something rather baffling in C# just recently. In our code base, we have a TreeNode class. When changing some code, I found that it was impossible to assign a variable to the Nodes property. On closer inspection it became clear that the property is read-only and this behavior is to be expected.

What is strange is that our code base had until then always relied on assignment of some anonymous type to the Nodes property and compiled and worked perfectly.

To summarize: why did the assignment in AddSomeNodes work in the first place?

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
             };
        }
    }
}
like image 204
Michiel Avatar asked Jul 10 '12 14:07

Michiel


1 Answers

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.

like image 132
Adam Houldsworth Avatar answered Sep 25 '22 19:09

Adam Houldsworth