zoukankan      html  css  js  c++  java
  • Creating Collection Classes in C#

    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 which 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 store 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 definitions 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 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 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!

    About the author

    Name :

    Bipin Joshi

    Email :

    webmaster@dotnetbips.com

    Profile :

    Bipin Joshi is the webmaster of DotNetBips.com. He is the founder of BinaryIntellect Consulting (www.binaryintellect.com) - a company providing training and consulting services on .NET framework. He conducts intensive training programs in Thane/Mumbai for developers. He is also a Microsoft MVP (ASP.NET) and a member of ASPInsiders.

  • 相关阅读:
    Java 标记接口
    数据结构 红黑树
    项目实践总结 存储短信验证码
    Java Web 浏览器关闭后Session就会被销毁吗?
    Java Web 禁用Cookie对Session的影响
    Java 接口 新特性(Java8)
    项目实践总结 修改个人资料时避免不必要的修改
    Java 多线程 sleep()方法与yield()方法的区别
    续XX后对一些想法的认同(两则)
    POLYCOM Group 310 远程视频会议系统项目 高清视频会议终端
  • 原文地址:https://www.cnblogs.com/zsxfbj/p/175297.html
Copyright © 2011-2022 走看看