程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 使用C# Indexer

使用C# Indexer

編輯:關於C語言
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);
                }
        } 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved