Maha

Maha

  • NA
  • 0
  • 328.3k

NP94 class within the class

Apr 15 2008 4:44 PM

Hi Guys

 

NP94 class within the class

 

http://msdn2.microsoft.com/en-us/library/aa288257(VS.71,printer).aspx

 

I got this program from the above website. This program contains a class within the class. I wish to know whether this is a common phenomenon or special case like this. 

Anyone knows please explain.

 

Thank you

 

// statements_foreach_collections.cs

// Using foreach with C#-specific collections:

using System;

 

// Declare the collection:

public class MyCollection

{

   int[] items;

 

   public MyCollection()

   {

      items = new int[5] { 12, 44, 33, 2, 50 };

   }

 

   public MyEnumerator GetEnumerator()

   {

      return new MyEnumerator(this);

   }

 

   // Declare the enumerator class:

   public class MyEnumerator

   {

      int nIndex;

      MyCollection collection;

 

      public MyEnumerator(MyCollection coll)

      {

         collection = coll;

         nIndex = -1;

      }

 

      public bool MoveNext()

      {

         nIndex++;

         return (nIndex < collection.items.GetLength(0));

      }

 

      public int Current

      {

         get

         {

            return (collection.items[nIndex]);

         }

      }

   }

}

 

public class MainClass

{

   public static void Main()

   {

      MyCollection col = new MyCollection();

      Console.WriteLine("Values in the collection are:");

 

      // Display collection items:

      foreach (int i in col)

      {

         Console.WriteLine(i);

      }

   }

}

/*

Values in the collection are:

12

44

33

2

50

*/


Answers (2)