New Features in C# 2.0—Enumerate Using Generic Iterators
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Tutorials |
| C# Tutorials |
| © 2005 O'Reilly Media, Inc. |
Enumerate Using Generic Iterators
In the previous examples you could not iterate over your list of Pilgrims
using a foreach loop. As such, if you try to use the following code in Example 1-3:
foreach ( Pilgrim p in pilgrims ) { Console.WriteLine("The pilgrim's name is " + p.ToString( )); }
you will receive the following error:
Error 1 foreach statement cannot operate on variables of type
'ImplementingGenericInterfaces.LinkedList <ImplementingGenericInterfaces.
Pilgrim>' because 'ImplementingGenericInterfaces.LinkedList
<ImplementingGenericInterfaces.Pilgrim>' does not contain a public
definition for 'GetEnumerator'
In earlier versions of C#, implementing GetEnumerator was somewhat
complicated and always tedious, but in C# 2.0 it is greatly simplified.
Adding iterators allows a client to iterate over your class using foreach.
|
|

