Not only List <T> has the right to foreach/ForEach, but we can easily implement it.
Just like this,
Public class ForEachSupport {
Static void Main (string [] args ){
Console. WriteLine ("foreach & ForEach support ");
EnumerableClass <Document> ec = new EnumerableClass <Document> ();
Ec. link (Document. create ("hello "));
Ec. link (Document. create ("world "));
Ec. link (Document. create ("!!! "));
Foreach (document each in EC ){
Console. writeline (each. Title );
}
EC. foreach (delegate (document each ){
Console. writeline (each. Title );
});
Console. Readline ();
}
}
Implementation is also very simple, Public class enumerableclass <t>: ienumerable <t> {
Private List <T> collection = new List <T> ();
Public IEnumerator <T> GetEnumerator (){
Foreach (T each in collection ){
Yield return each;
}
}
IEnumerator IEnumerable. GetEnumerator (){
Return GetEnumerator ();
}
Public void ForEach (Action <T> action ){
Collection. ForEach (action );
}
Public void link (T entity ){
Collection. Add (entity );
}
}
That's OK, all of it.