2) The method like GetEnumerator() of IEnumerable interface has to be defined to iterate a collection.
3) There is 1 method of IEnumerable interface. Which is IEnumerator GetEnumerator (). (Return type is Ienumerator type)
4) It returns an IEnumerator object that can be used to iterate through the collection.
5) Its not required to implement IEnumerable interface though we are overriding the GetEnumerator(), because we are using Namespace System.Collections. So it’s not mandatory to right IEnumerable interface.
6) We should implement the IEnumerable to use foreach iteration.
7) There are 2 public methods of IEnumerator Interface.
8) MoveNext() advances the enumerator to the next element of the collection.
9) Reset() sets the enumerator to its initial position, which is before the first element in the collection.
10) MoveNext() returns bool. And the Syntax is bool MoveNext().
11) If it’s true then enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
12) After an enumerator is created or after the Reset method is called, an enumerator is positioned before the first element of the collection, and the first call to the MoveNext method moves the enumerator over the first element of the collection.
If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false until Reset is called.
13) An enumerator remains valid as long as the collection remains unchanged.
14) If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException.
15) IEnumerator.Current Property gets the current element in the collection. Its syntax is Object Current { get; }
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Example:
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ienumeratordemo
{
class Employee
{
private string name;
private int empno;
public Employee(int empno, string name)
{
this.empno = empno;
this.name = name;
}
public int Empno
{
get { return empno; }
set { empno = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
}
EmployeeInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Ienumeratordemo
{
class EmployeeInfo //: IEnumerable
{
public Employee[] empArray = new Employee[3];
public EmployeeInfo()
{
empArray[0] = new Employee(1001, "Ajit");
empArray[1] = new Employee(1002, "Sunil");
empArray[2] = new Employee(1003, "Raj");
}
public IEnumerator GetEnumerator()
{
return empArray.GetEnumerator();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Ienumeratordemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("IEnumerable / IEnumerator Demo\n");
EmployeeInfo empinfoObj = new EmployeeInfo();
// Hand over each Employee in the collection ?
foreach (Employee e in empinfoObj)
{
Console.WriteLine("{0} \t {1}",e.Empno, e.Name);
}
//manually iterate
IEnumerator i = empinfoObj.GetEnumerator();
i.MoveNext();
Employee myEmployee = (Employee)i.Current;
Console.WriteLine("{0} \t {1}", myEmployee.Empno, myEmployee.Name);
i.Reset();
Console.WriteLine(" *******************************************");
IEnumerator i1 = empinfoObj.GetEnumerator();
i1.MoveNext();
i1.MoveNext();
i1.MoveNext();
Employee myEmployee1 = (Employee)i1.Current;
Console.WriteLine("{0} \t {1}", myEmployee1.Empno, myEmployee1.Name);
Console.Read();
}
}
}
Output :
No comments:
Post a Comment