introduction collection classes are used frequently in .net. for example, classes like arraylist, namevaluecollection, hashtable are collection classes. one peculiar thing of collection classes is that they can be used in foreach(...) loop in c#. vb.net has similar construct for...each. your custom classes can also be iterated in this way if you implement certain interfaces. in this article we will see how to code collection classes using c#. sample class for our example we will assume that we need some data structure in whch we will store list of book titles. we should be able to add, remove and iterate through the set of book titles. we will create a collection class called books that will stire these titles. before going in to the details of creating collection class let us see the books class in its most basic form. namespace collectionclass { class books { private arraylist m_booktitles; public books() { m_booktitles=new arraylist(); }
public void add(string booktitle) { m_booktitles.add(booktitle); }
public void remove(string booktitle) { m_booktitles.remove(booktitle); } } } you will notice that we have provided easy way to add and remove items to the books class via arraylist. however, we can not iterate the class using foreach construct. in order to achieve this we have to :
implement system.collections.ienumerable interface in books class
create a class that implements system.collections.ienumerator interface
ienumerable interface ienumerable interface has only one method getenumerator() that returns a class that implements ienumerator interface. following code shows books class after implementing ienumerable interface. class books:ienumerable { public arraylist m_booktitles;
public books() { m_booktitles=new arraylist(); }
public void add(string booktitle) { m_booktitles.add(booktitle); }
public void remove(string booktitle) { m_booktitles.remove(booktitle); }
public ienumerator getenumerator() { return new booksenumerator(this); } } the getenumerator() method returns an instance of class booksenumerator that we will discussed next. ienumerator interface ienumerator interface resides in system.collection namespace. this interface has following method and property definations that we need to implement: public bool movenext() { } public void reset() { } public object current { } this movenext tells us what to do when user wants to scroll forward in our collection. reset sets the initial position of the collection i.e. -1. current retrieves current item from the collection. booksenumerator class here is complete code of a class that implements ienumerator interface. class booksenumerator : ienumerator { private int position = -1; private books books;
public booksenumerator(books books) { this.books = books; }
public object current { get { return books.m_booktitles[position]; } } } we passed our books class to the constructor of this class. this class then maintains a pointer to the current position in the collection. using your collection class you can now use your books collection class in your code as follows: class class1 { static void main(string[] args) { books b=new books();
b.add("vb.net"); b.add("c#"); b.add("asp.net");
foreach(string s in b) { console.writeline(s); } } } keep coding!