I have a node
class that basically consists of a parent
property and a list of childs.
There's an AddChild
method which shouldn't receive null as an argument. Users shall not add a null child.
But the parent
property must allow null values (a root node has a null parent).
The question is "How to forbid users to add null children at compile-time
?". I know I could add a NullArgumentException
, but that is not what I need.
Another way around would be creating a List that doesn't accept null items (compile-time).
Code:
class Node
{
public Node Parent { get; set; }
private List<Node> Children = new List<Node>();
public void AddChild<Maybe a generics solution?>(Node???? Child) where ????
{
//I don't want to add (if Child == null) Throw new ArgumentNullException();
Children.Add(Child);
}
}