Click here to Skip to main content
11,635,123 members (74,967 online)
Click here to Skip to main content

How to use the IEnumerable/IEnumerator interfaces

, 3 Apr 2012 CPOL 22.8K 11
Rate this:
Please Sign up or sign in to vote.
This article describes how to work with the IEnumerable/IEnumerator interface for collection types.

Introduction

IEnumerable is an interface implemented by the System.Collecetion type in .NET that provides the Iterator pattern. The definition according to MSDN is:

“Exposes the enumerator, which supports simple iteration over non-generic collections.”

It’s something that you can loop over. That might be a List or Array or anything else that supports a foreach loop. IEnumerator allows you to iterate over List or Array and process each element one by one.

Objective

Explore the usage of IEnumerable and IEnumerator for a user defined class.

Using the code

Let’s first show how both IEnumerable and IEnumerator work: Let’s define a List of strings and iterate each element using the iterator pattern.

// This is a collection that eventually we will use an Enumertor to loop through
// rather than a typical index number if we used a for loop.
string[] Continents = new string[] { "Asia", "Europe", "Africa", "North America", "South America", "Australia", "Antartica" };
 

Now we already knows how to iterate each element using a foreach loop:

// Here is where loop iterate over each item of collection
foreach(string continent in Continents)
{
    Console.WriteLine(continent);
}

The same can be done with the IEnumerator object.

// HERE is where the Enumerator is gotten from the List<string> object

IEnumerator enumerator = Continents.GetEnumerator()

while(enumerator.MoveNext())
{
    string continent = Convert.ToString(enumerator.Current);
    Console.WriteLine(continent);
}</string>

Points of Interest

That's the first advantage: if your methods accept an IEnumerable rather than an Array or List, they become more powerful because you can pass different kinds of objects to them.

The second and most important advantage over List and Array is, unlike List and Array, an iterator block holds a single item in memory, so if you are reading the result from a large SQL query, you can limit your memory use to a single record. Moreover this evaluation is lazy. So if you're doing a complicated work to evaluate the enumerable as you read from it, the work doesn't happen until it's asked for.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Himanshu Manjarawala
Team Leader Automation Anywhere Inc.
India India
I am Himanshu Manjarawala, Garduate in Computer Science and MCA From Veer Narmad South Gujarat University, Surat Gijarat India. Currently working as Sr. Software Developer in Automation Anywhere Softwares Pvt. Ltd. Vadodara, Gujarat

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
BillWoodruff3-Jan-15 18:56
professionalBillWoodruff3-Jan-15 18:56 
QuestionAdd some examples Pin
Member 914033627-Sep-13 0:50
memberMember 914033627-Sep-13 0:50 
GeneralMy vote of 4 Pin
SaiKiran.Mandhala28-Apr-13 20:13
memberSaiKiran.Mandhala28-Apr-13 20:13 
SuggestionLacking important info. Inconsistent: Generic/non-Generic Pin
Matt T Heffron2-Apr-12 10:37
memberMatt T Heffron2-Apr-12 10:37 
GeneralRe: Lacking important info. Inconsistent: Generic/non-Generic Pin
ThatsAlok2-Apr-12 19:54
memberThatsAlok2-Apr-12 19:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 3 Apr 2012
Article Copyright 2012 by Himanshu Manjarawala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid