Introduction

The IEnumerable interface is the base Interface for many collections in C#, and its job is to provide a mode of iteration through a collection. That is why we tin utilize for each loops to go through a List or a Lexicon. In simple English when a collection class implements the IEnumerable interface it becomes countable, and we can count each element in it individually

There are 2 versions of the IEnumerable Interface

1.IEnumerable <T> for generic collections

two.IEnumerable for nongeneric collections

But since generic collections were introduced after after the non-generic ones and it is no longer recommended to employ the non-generic collections in a new project due to their need to perform boxing and unboxing (converting the types of objects), we volition explicate how to use the

IEnumerable <T> interface in this lesson

IEnumerable<T> contains a single method that you must implement when implementing this interface; GetEnumerator(), which returns an IEnumerator<T> object. The returned IEnumerator<T> provides the ability to iterate through the collection by exposing a Current property that points at the object we are currently at in the drove.

IEnumerables accept a method to get the next particular in the collection, so they look at items "one at a time," they don't need the whole collection to be in memory. They don't know how many items are in it for each loop. Information technology but keeps getting the next item until it runs out. IEnumerables besides aid ensure immutability (read-just)

when information technology is recommended to apply the IEnumerable interface:

  • Your collection represents a massive database table, you don't want to copy the entire matter into memory and cause performance issues in your application.

When it is not recommended to employ the IEnumerable interface:

  • You lot need the results right abroad and are mayhap mutating/editing the objects after on. In this case, it is better to use an Array or a List

Making your Classes Enumerable

In this case, we will see how to brand our course Enumerable meaning we can use an instance of it inside a for each loop

let'due south consider the post-obit classes

Class 1: a class chosen dog which has two properties

  1. string Name
  2. bool IsNaughtyDog

and ane method

  1. GiveTreat() which volition cause the dog to say "wuf" on the console :)

Class 2: a grade chosen DogShelter

Which contains the List of dogs and that'due south it

The List of dogs is initialized in the constructor

Now in the main method let's create a new DogShelter object and try to Enumerate through information technology

using a for each loop

We will Immediately see an fault , This error is proverb that our class lacks the ability to be Enumerated through

To fix this we demand to implement the interface IEnumerbale<T> where T is going to be the type of objects that we will enumerate through which in our instance is Dog

first, since this interface is concerned with the generic collections, we demand to import the generic collections namespace

after nosotros implement the interface IEnumerable<Dog> our class should look similar this

Now the mistake within the for each loop is gone, but another fault has appeared on the IEnumerable<Domestic dog>

Information technology is lament considering we did not implement the ii methods that are inside the IEnumerable Interface, which are

  1. IEnumerable<Dog>.GetEnumerator() which is the method nosotros need to implement to make our class Enumerable.
  2. IEnumerable.GetEnumerator() which is the non-generic version of the get-go method

Now the reason we need to implement the second method (non-generic version) is to ensure backward compatibility with the non-generic collections just in instance because the generic collections came after the not-generic collections

Let's striking testify potential fixes

and then select Implement Interface

These will the 2 methods we mentioned above in our class

The course should look like this afterward

Now since generic collections implement the IEnumerable Interface, then they also have their ain implementation of the GetEnumerator() method, Which means in our instance since the DogShelter uses a Listing<Dog> every bit a collection then we can utilize the GetEnumerator() of that list equally a workaround

Our DogShelter course should then expect like this

Complete Code