Function of GetEnumerator() in CSHARP
Why we use GetEnumerator( ) of Ienumerable interface
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Nov 17, 2011, 12:37 AM
Please check below link for more information.
http://www.functionx.com/csharp/Lesson29.htm
1.Unlike NET Framework facilitates accessing individual elements in the custom collection while implementing IEnumerable and IEnumerator
interfaces. For example, it is a hassle free job to access types collected in an ArrayList. This had been eased by ArrayList class while implementing
IEnumerable and IEnumerator interfaces
2. The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call.
This IEnumerator interface will allow us to iterate through any custom collection.
3.The IEnumerator interface is returned by GetEnumerator() method. All .Net collections implement The IEnumerable interface and the GetEnumerator() method.
4.Exposes the enumerator, which supports a simple iteration over a non-generic collection.
5.IEnumerable must be implemented to support the foreach semantics of Microsoft Visual Basic. COM classes that allow enumerators also implement this interface.
6.It's the interface that enumerates all the elements in a sequence .
Hope this will help you.
Thanks.
Datta KharadPosted Nov 17, 2011, 12:36 AM
Let's examine the most common and easiest way to loop through a Dictionary instance. The foreach loop here actually compiles into intermediate language that uses the GetEnumerator method, MoveNext, and Current, as well as a try/finally block.
Finally
Looping over Dictionary with foreach [C#]
static int A(Dictionary
{
int a = 0;
foreach (var pair in d)
{
a += pair.Value;
}
return a;
}
Next, this method demonstrates the GetEnumerator and MoveNext methods and the Current property directly. This code is compiled to the same intermediate language except the try/finally block is absent.
Looping over Dictionary with GetEnumerator [C#]
static int B(Dictionary
{
int b = 0;
var enumerator = d.GetEnumerator();
while (enumerator.MoveNext())
{
var pair = enumerator.Current;
b += pair.Value;
}
return b;
}
What's the difference? The difference between method A and method B is that method B is harder to read and it also omits the try/finally construct that was added by the C# compiler. Therefore, the next question is, "Which one is faster?"
Performance
I tested the two methods of looping through Dictionary instances in a test program. The harness used is available in the link below. The dictionary initialization code is shown below as well, and finally we see that method B was faster by a fairly constant amount.
Example:-
var d = new Dictionary
d["one"] = 1;
d["two"] = 2;
d["three"] = 3;
d["four"] = 4;
d["five"] = 5;
Results:-
Method A (foreach): 92.07 ns
Method B (GetEnumerator): 88.53 ns
Summary
Using GetEnumerator directly is somewhat faster than using the foreach loop on Dictionary. The finally statement actually calls the enumerators Dispose method, which has an empty implementation. Thus, GetEnumerator is worth considering as a way to improve Dictionary performance in certain C# programs.