Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple IEnumerator use (with example)

Tags:

c#

ienumerator

I am having trouble remembering how (but not why) to use IEnumerators in C#. I am used to Java with its wonderful documentation that explains everything to beginners quite nicely. So please, bear with me.

I have tried learning from other answers on these boards to no avail. Rather than ask a generic question that has already been asked before, I have a specific example that would clarify things for me.

Suppose I have a method that needs to be passed an IEnumerable<String> object. All the method needs to do is concatenate the letters roxxors to the end of every String in the iterator. It then will return this new iterator (of course the original IEnumerable object is left as it was).

How would I go about this? The answer here should help many with basic questions about these objects in addition to me, of course.

like image 721
BlackVegetable Avatar asked Sep 05 '11 16:09

BlackVegetable


People also ask

What is the use of IEnumerator?

IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.

What is the use of GetEnumerator in C#?

The C# GetEnumerator() method is used to convert string object into char enumerator. It returns instance of CharEnumerator. So, you can iterate string through loop.

What is the difference between IEnumerable and IEnumerator?

An IEnumerator is a thing that can enumerate: it has the Current property and the MoveNext and Reset methods (which in . NET code you probably won't call explicitly, though you could). An IEnumerable is a thing that can be enumerated...which simply means that it has a GetEnumerator method that returns an IEnumerator .


2 Answers

Here is the documentation on IEnumerator. They are used to get the values of lists, where the length is not necessarily known ahead of time (even though it could be). The word comes from enumerate, which means "to count off or name one by one".

IEnumerator and IEnumerator<T> is provided by all IEnumerable and IEnumerable<T> interfaces (the latter providing both) in .NET via GetEnumerator(). This is important because the foreach statement is designed to work directly with enumerators through those interface methods.

So for example:

IEnumerator enumerator = enumerable.GetEnumerator();  while (enumerator.MoveNext()) {     object item = enumerator.Current;     // Perform logic on the item } 

Becomes:

foreach(object item in enumerable) {     // Perform logic on the item } 

As to your specific scenario, almost all collections in .NET implement IEnumerable. Because of that, you can do the following:

public IEnumerator Enumerate(IEnumerable enumerable) {     // List implements IEnumerable, but could be any collection.     List<string> list = new List<string>();       foreach(string value in enumerable)     {         list.Add(value + "roxxors");     }     return list.GetEnumerator(); } 
like image 129
Paul Walls Avatar answered Sep 21 '22 02:09

Paul Walls


public IEnumerable<string> Appender(IEnumerable<string> strings) {   List<string> myList = new List<string>();   foreach(string str in strings)   {       myList.Add(str + "roxxors");   }   return myList; } 

or

public IEnumerable<string> Appender(IEnumerable<string> strings) {   foreach(string str in strings)   {       yield return str + "roxxors";   } } 

using the yield construct, or simply

var newCollection = strings.Select(str => str + "roxxors"); //(*) 

or

var newCollection = from str in strings select str + "roxxors"; //(**) 

where the two latter use LINQ and (**) is just syntactic sugar for (*).

like image 37
Rune Avatar answered Sep 20 '22 02:09

Rune