|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionLike C# provides indexers to index elements within objects, C# also provides us with Enumerators to allow us enumerate through the elements within the objects. We need to use an Interface Seems like the technical jargon is undigestible? Let's understand what we wish to do through small examples. Consider the following example. P1.csclass Demo
{
public static void Main ()
{
string [] a = new string[3] {"One" , "Two" ,"Three"};
//Using the foreach loop
foreach( string s in a)
System.Console.WriteLine(s);
// Using the Enumerator Interface
System.Collections.IEnumerator en = a.GetEnumerator();
while(en.MoveNext())
System.Console.WriteLine(en.Current);
}
}
OutputOne
Two
Three
One
Two
Three
The above program compiles and runs successfully to give the desired output. The above program contains an array of three strings. We print the array using Our goal is to have objects of a class use We wish to have a program such that it creates an object of a class and uses it in the P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
Ok! So in our program we have a class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str
{
}
We should be able to use object of class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
}
OK. So we have a class For our class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four","five"};
}
P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
public IEnumerator GetEnumerator( )
{
IEnumerator r = new StrEnumerator();
return r ;
}
}
OK. We implimented the Now let's look at the code of the OK! So we need to have a class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
public IEnumerator GetEnumerator( )
{
IEnumerator r = new StrEnumerator();
return r ;
}
}
class StrEnumerator
{
}
Our class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
public IEnumerator GetEnumerator( )
{
IEnumerator r = new StrEnumerator();
return r ;
}
class StrEnumerator
{
}
}
OK! Now let's have data members for class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
public IEnumerator GetEnumerator( )
{
IEnumerator r = new StrEnumerator();
return r ;
}
class StrEnumerator
{
int index;
Str sp;
}
}
The integer index is required to keep a count of elements as we enumerate each element. P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
public IEnumerator GetEnumerator( )
{
IEnumerator r = new StrEnumerator(this);
return r ;
}
class StrEnumerator
{
int index;
Str sp;
public StrEnumerator (Str str_obj)
{
index = -1 ;
sp = str_obj ;
}
}
}
Let's say that OK. For our class P2.csusing System ;
using System.Collections ;
class Demo
{
public static void Main ()
{
Str spp = new Str();
foreach( string i in spp)
System.Console.WriteLine(i);
}
}
class Str : IEnumerable // To impliment GetEnumerator method
{
string [] str_arr = new string[5] {"one" , "two" ,"three", "four", "five"};
public IEnumerator GetEnumerator( )
{
IEnumerator r = new StrEnumerator(this);
return r ;
}
class StrEnumerator
{
int index;
Str sp;
public StrEnumerator (Str str_obj)
{
index = -1 ;
sp = str_obj ;
}
public object Current
{
get
{
return sp.str_arr[ index ] ;
}
}
public bool MoveNext( )
{
if ( index < sp.str_arr.Length - 1 )
{
index++ ;
return true ;
}
return false ;
}
public void Reset( )
{
index = -1 ;
}
}
}
Ok! We are Done! Compile and Run the program. The program Compiles and runs successfully to give the following output. one
two
three
four
five
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||