C#
  Home arrow C# arrow Page 3 - Interface, IEnumerable and IEnumerator in ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Interface, IEnumerable and IEnumerator in C#
By: James Yang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 80
    2002-05-15

    Table of Contents:
  • Interface, IEnumerable and IEnumerator in C#
  • Object Oriented Programming
  • Using interfaces
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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
    {
    ..members
    }


    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)
    {
    this.customerID=customerID;
    }
    private int customerID;
    public int CustomerID
    {
    get
    {
    return customerID;
    }
    set
    {
    customerID =value;
    }
    }
    private string name;
    public string Name
    {
    get
    {
    return name;
    }
    set
    {
    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

    this.Reset();
    }
    public void SortByID()
    {
    // Get all customerIDs from the database sorted by customerID

    this.Reset();
    }
    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()
    {
    ienum.Reset();
    }
    public bool MoveNext()
    {
    return ienum.MoveNext();
    }
    public object Current
    {
    get
    {
    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.

    More C# Articles
    More By James Yang


       · Nice article I got to say. Being an intermediate level developer I had a fair good...
     

    C# ARTICLES

    - 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#






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT