While storing and retrieving list of elements we use many classes for that like ICollection,IList .
IEnumerable<T>
The read only type of collection is used to repeat the IEnumerable<T>. It gives only one method named GetEnumerator() using which user can repeat the read only elements using foreach loop. IEnumerable <T> is defined in as follows:
public void PrintAgeUpto30(IEnumerator<int> age_IEnumerator)
{
while (age_IEnumerator.MoveNext()){
Console.WriteLine(age_IEnumerator.Current);
if (age_IEnumerator.Current > 20) {
Console.WriteLine("PrintGreaterThan30 is called");
PrintGreaterThan30(age_IEnumerator);
}
}
}
public void PrintGreaterThan30(IEnumerator<int> age_IEnumerator)
{
while (age_IEnumerator.MoveNext())
Console.WriteLine(age_IEnumerator.Current);
}
// Now Call PrintUpto30 which will call PrintGreaterThan30
// by using our previous age IEnumerator
PrintUpto30(IEnumerator);
We use IEnumerable when we don't want to add or remove element from a collection.
We don't have any scope for changing the contents while performing any iteration over it and for iteration only for each loop is used.
0 Comment(s)