Automatically set parent reference on child items
<p>I have a class that contains an <code>IList&lt;T&gt;</code> property called <code>Children</code>. Each child should reference its parent. My solution has been to set the <code>ChildClass.Parent</code> property in the <code>ParentClass.Children</code> getter. Is there a better way, or is this the best solution?</p>

<pre><code>class ParentClass
{
    private IList&lt;ChildClass&gt; _children;

    public virtual IList&lt;ChildClass&gt; Children
    {
        get
        {
            // make sure each child refers to its parent (this)
            foreach (ChildClass c in _children)
            {
                c.Parent = c.Parent ?? this;
            }
            return _children;
        }
        set
        {
            _children = value;
        }
    }
}
</code></pre>
