Home arrow C# arrow Page 3 - Interface, IEnumerable and IEnumerator in C#

Interface, IEnumerable and IEnumerator in C#

The concept of an interface is an important element in object-oriented programming. It's what makes OOP so structured but flexible at the same time. In this article James shows us what an interface is and also shows us how to implement some pre-defined interfaces using C#.

Author Info:
By: James Yang
Rating: 3 stars3 stars3 stars3 stars3 stars / 154
May 15, 2002
  1. · Interface, IEnumerable and IEnumerator in C#
  2. · Object Oriented Programming
  3. · Using interfaces
  4. · Conclusion

print this article

Interface, IEnumerable and IEnumerator in C# - Using interfaces
(Page 3 of 4 )

It's really easy to implement one or more interfaces, and technically because it is not a class, multiple interfaces can be implemented at once. An interface is implemented as if it was a class:

class AClass : Class, ISomething, IInterface, IMoreInterface

Interfaces provided by Microsoft
There are hundreds of interfaces that shipped with the .NET Framework SDK. Those interfaces are essential within the framework as they hold's the framework's structure and ensure compatibility between objects. They also allow 3rd party additions of objects to work flawlessly with Microsoft's prewritten library of objects.

IEnumerator and IEnumerable are the two most commonly used interfaces and we will discuss them in detail now.

These Interfaces reside in the System.Collections namespace.

Firstly let's have a look at each interface we are dealing with here. IEnumerator is an interface that has following members:
  • Property: Current
  • Method: MoveNext
  • Method: Reset
MSDN describes the IEnumerator class as "Supports a simple iteration over a collection.", as well as:

"IEnumerator is the base interface for all enumerators. Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. Reset also brings the enumerator back to this position. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

IEnumerable is an interface that has 1 method called GetEnumerator. The MSDN documentation describes the GetEnumerator method as "Exposes the enumerator, which supports a simple iteration over a collection."... It then goes on to say "IEnumerable must be implemented to support the ForEach semantics of Microsoft Visual Basic. COM classes that allow enumerators also implement this interface".

As you can see, MSDN plays an important role in .NET development by providing the developers with correct information. In this case, MSDN provides what each method or property in the interface should do when implemented by a class.

So now that we know about the interfaces we are dealing with, let's start making some collection objects. What we are going to make today is a collection called Customers, which will contain Customer objects. I chose this because this collection seemed to be used a lot.

First, we need to make a Customer class:

class Customer
public Customer(int customerID)
private int customerID;
public int CustomerID
return customerID;
customerID =value;
private string name;
public string Name
return name;
name =value;

This is a fairly simple class, and contains just two properties. One returns the customers name, and the other returns the customers id. Now, we want to make a Customer collection class that is enumerable but at the same time has methods to sort customers by different fields.

On top of the methods implemented by the interfaces, it will also have a method called
SortByName(), which will sort the items in the collection by the field 'name' and reset the collection.

There will be another method called SortByID(), which will sort the items in the collection by the field 'customerID' and then reset the collection.

Here's the code for this collection class:

class Customers : System.Collections.IEnumerable, System.Collections.IEnumerator
System.Collections.ArrayList customerIDs;
System.Collections.IEnumerator ienum;

public void SortByName()
// Get all customerIDs from the database sorted by name

public void SortByID()
// Get all customerIDs from the database sorted by customerID

public Customers()
// Get all customerIDs from the database and is put in to an ArrayList,
// customerIDs

// Gets ienumerator interface for customerIDs arraylist
ienum = customerIDs.GetEnumerator();
public System.Collections.IEnumerator GetEnumerator()
// Polymorph this object into an IEnumerator interface
return (System.Collections.IEnumerator)this;
public void Reset()
public bool MoveNext()
return ienum.MoveNext();
public object Current
return new Customer(Convert.ToInt32(ienum.Current));

Basically, the MoveNext and Reset methods are the MoveNext and Reset methods of the ArrayList that we've created which behind the scenes polymorphed into an IEnumerator interface and returned when GetEnumerator is called.

The Current property just returns a new Customer object, instantiated when it is called with the current object of the customerIDs ArrayList.
blog comments powered by Disqus

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials