国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Creating Collection Classes in C#

2024-07-21 02:22:06
字體:
供稿:網(wǎng)友
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 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 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!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 苗栗县| 张家界市| 广昌县| 石泉县| 育儿| 东辽县| 黄冈市| 丹阳市| 柳江县| 邵武市| 泗水县| 迭部县| 泌阳县| 依兰县| 池州市| 武冈市| 甘南县| 乌恰县| 和硕县| 南平市| 皋兰县| 乐安县| 丹凤县| 静乐县| 遂宁市| 夏河县| 绥德县| 樟树市| 肇东市| 池州市| 民权县| 当阳市| 郯城县| 长春市| 环江| 博爱县| 吴江市| 冷水江市| 克拉玛依市| 玉树县| 岗巴县|