Is there a c# language construct that will allow me to add items to a readonly collection property in a constructor? I want to do something like this:
public cl
If you have a property of type List that is get only, that only means you can't set that property, you can still add things to the list.
You could however expose an IEnumerable property instead and have a constructor that takes a list(or another IEnumerable more likely).
Property initializers do not work since the compiler will just rewrite them to regular property assignments.
I'd do this:
public class Node{
public IEnumerable Children {get; private set;}
public Node(IEnumerable children){
Children = children.ToList();
}
}
if you can't change the Node class, I suggest writing a helper class similar to this:
public static Node Create(IEnumerable children)
{
var n = new Node();
foreach (var c in children)
n.Children.Add(c);
return n;
}