DEV Community

yashaswi
yashaswi

Posted on

2

[Post 2]LINQ -IEnumerator

[Post-2] [LINQ] IEnumerator

Now that we have talked about *“Enumerable” .Lets talk about *“Enumerator”.

Enumerators are those that make a data structure Enumerable.

I.e. Enumerators help traverse or iterate an Enumerable. IEnumerable and IEnumerator implement the “Iterator Pattern”.

**Iterator** is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

Making it simple Iterator pattern asks us to separate the underlying data collection and the functionality to traverse it.

Below are the IEnumerator and IEnumerable interfaces. IEnumerable exposes a method which returns an IEnumerator. IEnumerator provides following methods and properties.

bool MoveNext(): Moves the pointer to next item in the collection and returns true. If the pointer is at the end of collection it returns false.

**object Current: **This property provides the current values of the item the pointer is located at.

**void Reset(): **Although not extensively used or is part of *Iterator pattern *UML diagram, this method sets the pointer to the initial value of the collection.

Look at the below example where different data structures return their Enumerators using which the these are iterated.

using System;
using System.Collections;
using System.Collections.Generic;
namespace LINQ
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
EnumerableAndEnumerator eAe = new EnumerableAndEnumerator();
Enumerate(eAe.GetEnumerators());
}
public static void Enumerate(List<IEnumerator> enumerators ) {
foreach (IEnumerator e in enumerators) {
while (e.MoveNext()) {
Console.WriteLine(e.Current);
}
}
Console.WriteLine("------------------------------------------");
}
}
public class EnumerableAndEnumerator
{
public List<IEnumerator> GetEnumerators()
{
List<IEnumerator> enumerators = new List<IEnumerator>();
int[] arr = { 1, 2, 3, 4, 5 };
enumerators.Add(arr.GetEnumerator());
List<int> list = new List<int>() { 6, 7, 8, 9, 10 };
enumerators.Add(list.GetEnumerator());
Dictionary<int, int> dictionary = new Dictionary<int, int>() { { 11, 11 }, { 12, 12 }, { 13, 13 }, { 14, 14 } };
enumerators.Add(dictionary.GetEnumerator());
Stack<int> stack = new Stack<int>();
stack.Push(15);
stack.Push(16);
enumerators.Add(stack.GetEnumerator());
Queue queue = new Queue();
queue.Enqueue(17);
queue.Enqueue(18);
enumerators.Add(queue.GetEnumerator());
return enumerators;
}
}
}
view raw Enumeation hosted with ❤ by GitHub

If you notice, in the above code we are iterating different data structures using same method “Enumerate”. If you get back to the *Iterator Pattern *definition, irrespective of the characteristics or internal implementation of the data structure we are able to iterate them uniformly.

Although it is not a common practice to use the enumerator for iterating a data structure. [Instead we would use the foreach *loop directly.] Understanding this particular concept helps in understanding **LINQ*.

In the next post lets look into **Yield **and how IEnumerable and Yield are linked together.

Thanks, please subscribe to the publication for more interesting posts.

[References]

Iterator Pattern : *https://www.youtube.com/watch?v=uNTNEfwYXhI*

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay