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

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

使用C# Indexer

2024-07-21 02:22:08
字體:
供稿:網(wǎng)友
indexer是c#中新增加的,可能有些朋友會有些困惑,最近做的一些事情
經(jīng)常使用index,順手寫一個(gè)簡單的例子給大家看看,如果有什么問題可以問!
  
這個(gè)例子包含三個(gè)類: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開發(fā)資源網(wǎng)站及技術(shù)社區(qū),
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 郯城县| 江源县| 定兴县| 榆树市| 吴堡县| 台东市| 白水县| 白玉县| 营山县| 浙江省| 洛川县| 沧州市| 六枝特区| 磐石市| 阿鲁科尔沁旗| 修武县| 鄂温| 瓮安县| 静海县| 温州市| 平潭县| 登封市| 罗山县| 雅安市| 新乡市| 绍兴市| 峨边| 金乡县| 九江市| 清苑县| 甘孜县| 牡丹江市| 天气| 长葛市| 屏山县| 涟源市| 西安市| 清镇市| 准格尔旗| 策勒县| 霍城县|