Day 8: What is IEnumerable?
Hello there!👋👋👋 Today I’m gonna share with you one of the cool types that .NET provides for storing and managing collection of objects. We already talk about arrays and List but I want to take the space to blog about the IEnumerable interface.
In .NET we can divide the types for collections into 3 categories:
- Interfaces that define standard collection protocols.
- Ready-to-use collection classes (list, dictionaries, etc.)
- Base classes for writing application specific collections
This time we are going to focus on the first one. Exposes an enumerator, which supports a simple iteration over a non-generic collection. IEnumerable contains a single method, GetEnumerator, which returns an IEnumerator interface. IEnumerator provides the ability to iterate through the collection with a for-each statement.
Key Points:
- IEnumerable interface is a generic interface which allows looping over generic or non-generic lists.
- IEnumerable interface also works with linq query expression.
- IEnumerable interface Returns an enumerator that iterates through the collection.
- IEnumerable<T> is the base interface for collections in the System.Collections.Generic namespace
We can implement IEnumerable Interface this way:
Conclusions
We use IEnumerable interface for one of the following reasons:
- To support the for-each statement.
- To interoperate with anything expecting a standard collection.
- To meet the requirements of a more sophisticated collection interface.
- To support collection initializers.
If you enjoyed this article, please hold down the clap button 👏👏👏 Don’t forget to follow our Coding winter Calendar. Have a wonderful day! 🧡 💛 💚🧡 💛 💚
If you want to get more information about all the methods and characteristics of them, follow this link: