Submitted by daniel newman (not verified) on Sat, 2009-11-28 18:46.
Why would we need to?
Lets say i have a list of rectangle objects i need to enumerate through via foreach.
so we implement the enumerable interface with Link in such a way using List<T>.
public class RectangleStartingPoints<T> : IEnumerable<T>
{
List<T> _items = new List<T>();
public void Add(T item)
{
_items.Add(item);
}
public IEnumerator<T> GetEnumerator()
{
foreach (T item in _items)
{
yield return item;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
since we've implemented using List<T> we can foreach all we want. or maybe i missed something?
so we implement the enumerable interface with Link in such a way
Why would we need to?
Lets say i have a list of rectangle objects i need to enumerate through via foreach. so we implement the enumerable interface with Link in such a way using List<T>.
public class RectangleStartingPoints<T> : IEnumerable<T> { List<T> _items = new List<T>(); public void Add(T item) { _items.Add(item); } public IEnumerator<T> GetEnumerator() { foreach (T item in _items) { yield return item; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }since we've implemented using List<T> we can foreach all we want. or maybe i missed something?