首页 > 开发 > 综合 > 正文

使用C# Indexer

2024-07-21 02:22:08
字体:
来源:转载
供稿:网友
indexer是c#中新增加的,可能有些朋友会有些困惑,最近做的一些事情
经常使用index,顺手写一个简单的例子给大家看看,如果有什么问题可以问!
  
这个例子包含三个类:datarow、dataitem、dataitemcollection。
其中dataitemcollection中使用了indexer。
  
using system;
using system.collections;
public static void main(string [] args)
{
        datarow mydatarow = new datarow();
        dataitem mydataitem = new dataitem("text","value");
        datarow.dataitems.add(mydataitem);
        console.writeline(mydatarow.dataitems[0].text);
}
        public class datarow
        {
                public dataitemcollection dataitems;
                public datarow()
                {
                        dataitems = new dataitemcollection();
                }
        }
  
    public class dataitem
    {
                private string _text;
                private string _value;
  
        public dataitem(string text, string value)
        {
                        _text=text;
                        _value=value;
        }
  
                public string text
                {
                        get
                        {
                                return(_text);
                        }
                        set
                        {
                                _text=value;
                        }
                }
  
                public string value
                {
                        get
                        {
                                return(_value);
                        }
                        set
                        {
                                _value=value;
                        }
                }
  
        }
  
        public class dataitemcollection
        {
                private arraylist _array = new arraylist();
  
                public virtual int count
                {
                        get
                        {
                                return(_array.count);
                        }
                }
  
                public dataitem this[int index] //此处使用了indexer
                {
                        get
                        {
                                if(index>=0 && index<_array.count)
                                {
                                        return((dataitem)_array[index]);
                                }
                                else
                                {
                                        throw new exception("index overflow");
                                }
                        }
  
                        set
                        {
                                if(index>=0 && index<_array.count)
                                {
                                        _array[index]=value;
                                }
                                else
                                {
                                        throw new exception("index overflow");
                                }
                        }
                }
  
                public void add(dataitem item)
                {
                        _array.add(item);
                }
  

                public void remove(dataitem item)
                {
                        _array.remove(item);
                }
  
                public void removat(int i)
                {
                        _array.removeat(i);
                }
        } 中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表