Sunday, October 20, 2019

Difference between IEnumerable and IEnumerator

IEnumerable and IEnumerator both are interface.

Relation

IEnumerable is simply a list and having one method called GetEnumerator() which gets the another type of interface called IEnumerator.

Syntax

Lets check the first demonstration of code.

IEnumerable


In the above code we simply create a list of weeks and iterate it over the foreach loop.
Then following are the output.

In the above code we simply create a list of weeks and iterate it over the MoveNext() method.
Then the following output are










Syntax are different but output are same.

How Enumerator are used over Enumerable?

In some cases we don't need to iterate over each and every value of collection object but we want to iterate some value in this collection object and remaining values will iterate over second business logic.

  Let take an example for this.



In the above code value is iterating over while loop till "Thursday" value and then after iterate the value to the second method i.e, "EnumeratorWeekSecond".


No comments:

Post a Comment