/// <summary> /// Inserts an item into the collection at the specified index. /// </summary> /// <param name="index">The index at which to insert the item.</param> /// <param name="item">The item to insert into the collection.</param> public override void Insert(int index, RecursiveCollection <T> item) { //Note: No error handling necessary. // If the index is out of bounds, then let the list's insert method throw an exception. base.Insert(index, item); item.Parent = this; }
/// <summary> /// Removes the first occurrence of a specific item from the collection. /// </summary> /// <param name="item">The item to remove from the collection.</param> /// <returns> /// Returns true if the item was successfully removed from the collection, otherwise false. /// This method also returns false if item was not found in the collection. /// </returns> public override bool Remove(RecursiveCollection <T> item) { bool removed = base.Remove(item); if (removed) { if (item.Parent == this && !Contains(item)) { item.Parent = null; } } return(removed); }
/// <summary> /// Removes all items from the collection. /// </summary> public override void Clear() { var oldItems = new RecursiveCollection <T> [Count]; ListItems.CopyTo(oldItems, 0); base.Clear(); foreach (var item in oldItems) { if (item.Parent == this) { item.Parent = null; } } }
/// <summary> /// Creates a new instance of <see cref="RecursiveCollection{T}"/>. /// </summary> /// <param name="value"> /// The value at this node of the <see cref="RecursiveCollection{T}"/>. /// </param> /// <param name="parent"> /// The collection of items that is the hierarchical parent of this collection. /// </param> /// <param name="childItems"> /// A collection of value to initialize this collection with, as /// the hierarchical children of this node. /// </param> public RecursiveCollection(T value, RecursiveCollection <T> parent, params T[] childItems) { Value = value; Parent = parent; Children = new List <RecursiveCollection <T> >(childItems.Select(childItem => new RecursiveCollection <T>(childItem, this))); }
/// <summary> /// Creates a new instance of <see cref="RecursiveCollection{T}"/>. /// </summary> /// <param name="value"> /// The value at this node of the <see cref="RecursiveCollection{T}"/>. /// </param> /// <param name="parent"> /// The collection of items that is the hierarchical parent of this collection. /// </param> public RecursiveCollection(T value, RecursiveCollection <T> parent) { Value = value; Parent = parent; Children = new List <RecursiveCollection <T> >(); }
/// <summary> /// Adds an item to the collection. /// </summary> /// <param name="item">The item to add.</param> public override void Add(RecursiveCollection <T> item) { base.Add(item); item.Parent = this; }