Implement IEnumerable Interface in C#

In this tutorial we will learn how to implement an IEnumerable interface in a C# class. If we implement the IEnumarable interface in our own class then we can loop through all class objects using a foreach loop.

Let's understand a very small example of the IEnumerable interface. In this example we will store a set of integer variables using IEnumerable.

The following  is a simple example:

using System;

using System.Collections.Generic;

using System.Reflection;

using System.Collections;

 

namespace TestDomain

{

    class Program

        {

            public static void Main(String[] args)

            {

 

                IEnumerable<int> Values = from value in Enumerable.Range(1, 10) select value;

                foreach (int a in Values)

                {

                    Console.WriteLine(a);

                }

 

                Console.ReadLine();

            }

        }

}

Here is sample output:

Implement-IEnumerable-interface

Let's see another example.

Let's implement another example of IEnumerable. In this example we will search a list using a LINQ query and we will go through all values using a foreach loop. Let's have a look at the following code.
 

using System;

using System.Text;

using System.Collections;

 

namespace TestDomain

{

        class Program

        {

            public static void Main(String[] args)

            {

                List<string> List = new List<string>();

                List.Add("Sourav");

                List.Add("Ram");

                List.Add("shyam");

                List.Add("Sachin");

 

                IEnumerable names = from n in List where (n.StartsWith("S")) select n;

 

                foreach (string name in names)

                {

                    Console.WriteLine(name);

                }

 

                Console.ReadLine();

            }

        }

}


This is the output:

Implement-IEnumerable-interface1

Implement IEnumerable in C# class

In this example we will implement an IEnumerable interface in our own C# class. If we implement the IEnumerable interface then we can loop through all the objects of it. Have a look at the following code.
 

using System;

using System.Diagnostics;

using System.Reflection;

using System.Collections;

 

namespace TestDomain

{

 

    class Test : IEnumerable

    {

        Test[] Items = null;

        int freeIndex = 0;

        public String Name { get; set; }

        public string Surname { get; set; }

 

        public Test()

        {

            Items = new Test[10];

        }

 

    public void Add(Test item)

    {

        Items[freeIndex] = item;

        freeIndex++;

    }

 

    // IEnumerable Member

    public IEnumerator GetEnumerator()

    {

        foreach (object o in Items)

        {

           

            if(o == null)

            {

                break;

            }

            yield return o;

        }

    }

    }

 

    class Program

        {

            public static void Main(String[] args)

            {

                Test t1 = new Test();

                t1.Name = "Sourav";

                t1.Surname = "Kayal";

 

                Test t2 = new Test();

                t2.Name = "Ram";

                t2.Surname = "Das";

 

 

                Test myList = new Test();

                myList.Add(t1);

                myList.Add(t2);

                foreach (Test obj in myList)

                {

                   Console.WriteLine("Name:- " +  obj.Name + "Surname :- " + obj.Surname);

                }

                Console.ReadLine();

            }

        }

}

Here is the output:

Output-Implement-IEnumerable-interface

We can see that in the Test class we implemented the IEnumerable interface. Within the constructor we are creating a few objects of the Test class and added a method to add the objects of the Test class one by one. The GetEnumerator method will return an object of the Test class one by one because we are using yield.

Conclusion

In this quick article we have tried to learn how to implement the IEnumerable interface in our own C# class. Hope you have understood the concepts.