The way compiler works with Enumerator

The interface IEnumerable contains ONLY method GetEnumerator() which will return IEnumerator of that collection type. IEnumerator interface contains two methods and one property
  1. object Current bool
  2.  MoveNext() 
  3. void Reset() 

Example:

List<string> name = new List<string>();
name.Add("Harvey");
name.Add("Dent");
name.Add("Clerik");

       foreach (var s in name)
            {

              Console.WriteLine ("Name: {0}", s);
         }


Now the above code will be manipulated by compiler as shown below:

IEnumerator<string> enumerator = name.GetEnumerator ();
while (enumerator.MoveNext())
{
string s = (string)enumerator.Current;
Console.WriteLine ("Name Compiler's Way: {0}", s);
}
source: Dhananjay Kumar
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment