/// <summary> /// Binds the node being inserted into the parent container according to the requested insertion position. /// </summary> /// <param name="parent">The double linked list collection to use to insert the current node into.</param> /// <param name="position">The preferred insertion order to use relative to the origin node.</param> /// <returns>The current node converted to actual node type when bound.</returns> protected TNode Bind(TList parent, InsertPosition position) { if (parent != null) { if (Parent == null) { Parent = parent; if (Parent.Head == null || (position == InsertPosition.Preceding && parent.Head == _next)) { Parent.Head = (TNode)this; } if (Parent.Tail == null || (position == InsertPosition.Following && Parent.Tail == _prev)) { Parent.Tail = (TNode)this; } Parent.Count++; Parent.AfterInserting((TNode)this); AfterInserting(); return((TNode)this); } else { throw new InvalidOperationException($"Can't bind the node that is linked to another list: {this}."); } } else { throw new ArgumentNullException(nameof(parent)); } }