新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:使用C# Indexer

使用C# Indexer

新客网 XKER.COM 2003-07-12 来源: 收藏本文
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);
                }
        } 
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐