---
title: "What is IEnumerable<> in C#?"  
description: "What is IEnumerable<> in C#?"  
author: "Anupam Mishra"  
published: 2016-01-27  
updated: 2016-01-28  
canonical: https://www.mindstick.com/forum/33934/what-is-ienumerable-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# What is IEnumerable<> in C#?

Hi Everyone,I am [now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) using [collections in c#](https://www.mindstick.com/forum/160388/describe-the-basic-features-and-use-cases-of-the-queue-and-stack-collections-in-c-sharp). So , i want to please [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) can give me a brief intro with [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) of IEnumerable<> interface.\
Thank You.

## Replies

### Reply by Anupam Mishra

In C# collection defines a non-generic IEnumerable interface and there is a generic type-safe IEnumerable<T> interface.

\
The IEnumerable interface is located in the System.[Collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp) namespace and contains only a single method definition. The interface definition looks like this:

```
public interface IEnumerable{    IEnumerator GetEnumerator();}
```

The GetEnumerator method must return an instance of an object of a class which implements the IEnumerator interface. I am not going to details. if you are interested, please visit the [msdn documentation](http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx).

It is important to know that the C# language foreach keyword works with all types that implement the IEnumerable interface. Only in C# [it also works with things that don’t explicitly implement IEnumerable or IEnumerable<T>](http://msdn.microsoft.com/en-us/library/9yb8xew9(v=vs.90).aspx). I believe you have been using the foreach keyword many times and without worrying about the reason why and how it worked with that type.

## IEnumerable<T>

Let’s now take a look at the definition of the generic and type-safe version called IEnumerable<T> which is located in the System.Collections.Generic namespace:

?

| public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } |
| --- |

As you can see the IEnumerable<T> interface inherits from the IEnumerable interface. Therefore a type which implements IEnumerable<T> has also to implement the members of IEnumerable.

IEnumerable<T> defines a single method GetEnumerator which returns an instance of an object that implements the IEnumerator<T> interface. We won’t have a look at this interface this time. Please take a look at the [msdn documentation](http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx) if you would like to get some more information.

```
IEnumerator<string> IEnumerable<string>.GetEnumerator()      //Generic    {      Console.WriteLine("Hi Guest");      return this.elements.GetEnumerator();    } //Non Generic     IEnumerator IEnumerable.GetEnumerator()    {      return this.elements.GetEnumerator();    }
```


---

Original Source: https://www.mindstick.com/forum/33934/what-is-ienumerable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
