Creating Collection Classes in C #
Introduction
Collection classes are used frequently in. NET. For example, classes like ArrayList, NameValueCollection, HashTable are. Collection. One peculiar thing of collection classes is this they can be used in foreach (...) loop in C #. VB.net has similar construct for ... Each. Your Custom classes can also is iterated in this way if you implement certain. In this article we'll be here to code collection classes using C #.
Sample Class
For our example we'll assume that we need some the data structure in WHCH we'll store list of book titles. We should be able to add, remove and iterate through the set's book titles. We'll create a collection class called books, that would stire these titles. Before going the details of the Creating collection class let us, 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'll 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 the books class
- Create A class that implements System.Collections.IEnumerator interface
IEnumerable Interface
IEnumerable interface has only one method GetEnumerator (), 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 to New Booksenumerator (this);
}
}
The GetEnumerator () method returns a instance of class Booksenumerator that we'll discussed next.
IEnumerator Interface
IEnumerator interface resides in System.Collection namespace. This interface has the following method and the property definations so we need to implement:
public bool MoveNext ()
{
}
public void Reset ()
{
}
public Object Current
{
}
This MoveNext tells us what to does 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
This is complete the code of a class that implements IEnumerator interface.
Class Booksenumerator:ienumerator
{
private int position =-1;
Private books and books;
Public Booksenumerator (books and books)
{
This.books = books;
}
public bool MoveNext ()
{
if (Position < Books.m_booktitles. COUNT-1)
{
position++;
return true;
}
Else
{
return false;
}
}
public void Reset ()
{
Position =-1;
}
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-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!