Is there any better/nicer way to do the following?
- An object that can either be used as a normal object and access it's properties and/or as an IEnumerable collection of the object. See code below:
Sample:
static void Main(string[] args)
{
Item i = new Item() {Name="Erik"};
i.Add(new Item() {Name="Fred"});
i.Add(new Item() {Name="Bert"});
// First access the object as collection and foreach it
Console.WriteLine("=> Collection of Items");
foreach (Item item in i)
Console.WriteLine(item.Name);
// Access a single property on the object that gives back the first element in the list.
Console.WriteLine("=> Single Item");
Console.WriteLine(i.Name);
Console.ReadLine();
}
public class Item : IEnumerable<Item>
{
public void Add(Item item)
{
_items.Add(item);
UpdateInnerList();
}
#region Fields
private List<Item> _items = new List<Item>();
private List<Item> _innerList = new List<Item>();
private string _name = string.Empty;
#endregion
public string Name
{
get { return _name; }
set
{
_name = value;
UpdateInnerList();
}
}
#region Methods
public IEnumerator<Item> GetEnumerator()
{
return _innerList.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _innerList.GetEnumerator();
}
private void UpdateInnerList()
{
_innerList.Clear();
_innerList.Add(this);
_innerList.AddRange(_items);
}
#endregion
}