日期:2012-08-01  浏览次数:20505 次

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;
                   &nb