Couldn’t find an answer to this question. It must be obvious, but still.
I try to use initializer in this simplified example:
MyNode newNode = new MyNode
{
NodeName = "newNode",
Children.Add(/*smth*/) // mistake is here
};
where Children is a property for this class, which returns a list. And here I come across a mistake, which goes like ‘Invalid initializer member declarator’.
What is wrong here, and how do you initialize such properties? Thanks a lot in advance!
You can’t call methods like that in object initializers – you can only set properties or fields, rather than call methods. However in this case you probably can still use object and collection initializer syntax:
Note that this won’t try to assign a new value to
Children, it will callChildren.Add(...), like this: