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
};
}
}
}
AddSomeNodesis not creating an instance ofList<TreeNode>because that syntax is a collection initializer (therefore it is not assigning toNodesmeaning it doesn’t break thereadonlycontract), the compiler actually translates the collection initializer into calls to.Add.The
AddSomeOtherNodescall actually tries to re-assign the value, but it isreadonly. 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 markedreadonly.From MSDN:
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:The MSDN documentation doesn’t seem to have any clarification on this.