C#中索引器簡單使用

微軟官方描述:索引器允許類或結構的實例就像數組一樣進行索引。索引器形態類似於,不同之處在於它們的取值函數採用參數。
通俗理解:索引器(Indexer) 允許一個對象可以像數組一樣被索引。當您爲類定義一個索引器時,該類的行爲就會像一個 虛擬數組(virtual array) 一樣。您可以使用數組訪問運算符([ ])來訪問該類的實例。
索引的代碼結構接近與類的屬性定義,都是通過get和set來進行相關業務操作,只是比屬性多了簽名,索引允許重載,索引支持多種數據類,可以通過對象+【簽名】來調用該索引

索引結構:

element-type this[int index] 
{
   // get 訪問器
   get 
   {
      // 返回 index 指定的值
   }

   // set 訪問器
   set 
   {
      // 設置 index 指定的值 
   }
}

索引簡單使用:

public class Indexer {

        public static int size = 10;
        private string[] array = new string[size];
        public Indexer() {
            for (int i= 0;i < 10;i++) {
                array[i] = "N/A";
            }
        }
        /// <summary>
        /// 給屬性設置或獲取屬性值
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public string this[int i] {
            get {
                if (i < 0 || i >= size) {
                    return "";
                }
                else {
                    return array[i];
                }
            }
            set {
                if (i < 0 || i >= size) {

                }
                else {
                     array[i]=value;
                }
            }
        }
        /// <summary>
        /// 查看當前屬性的值是否等於傳過來的值
        /// </summary>
        /// <param name="i"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool this[int i, string value] {
            get {
                if (i < 0 || i >= size) {
                    return false;
                }
                else {
                    if (array[i].Equals(value)) return true;
                    else return false; 
                }
            }
        }
    }

調用:

#region Indexer
            Indexer index = new Indexer();
            index[0] = "Zara";
            index[1] = "Riz";
            index[2] = "Nuha";
            index[3] = "Asif";
            index[4] = "Davinder";
            index[5] = "Sunil";
            index[6] = "Rubic";
            index[7] = "Rubic11";
            index[8] = "Rubic22";
            index[9] = "Rubic33";
            for (int i = 0; i < Indexer.size; i++) {
                Console.WriteLine(index[i]);
            }
            Console.WriteLine(index[1, "Riz"]);
            Console.ReadKey();
            #endregion

可以參考的例子:

namespace Study
{
    class Program
    {
        static void Main(string[] args)
        {
            ScoreIndex s = new ScoreIndex();
            s["張三", 1] = 90;
            s["張三", 2] = 100;
            s["張三", 3] = 80;
            s["李四", 1] = 60;
            s["李四", 2] = 70;
            s["李四", 3] = 50;
            Console.WriteLine("張三課程編號爲1的成績爲:" + s["張三",1]);
            Console.WriteLine("張三的所有成績爲:");
            ArrayList temp;
            temp = s["張三"];
            foreach (IndexClass b in temp) 
            {
                Console.WriteLine("姓名:" + b.Name + "課程編號:" + b.CourseID + "分數:" + b.Score);
            }
            Console.ReadKey();
        }
    }
    class IndexClass 
    {
        private string _name;
        private int _courseid;
        private int _score;
        public IndexClass(string _name, int _courseid, int _score) 
        {
            this._name = _name;
            this._courseid = _courseid;
            this._score = _score;
        }
        public string Name 
        {
            get { return _name; }
            set { this._name = value; }
        }
        public int CourseID 
        {
            get { return _courseid; }
            set { this._courseid = value; }
        }
        public int Score 
        {
            get { return _score; }
            set { this._score = value; }
        }
    }
    class ScoreIndex 
    {
        private ArrayList arr;
        public ScoreIndex() 
        {
            arr = new ArrayList();
        }
        public int this[string _name, int _courseid] 
        {
            get 
            {
                foreach (IndexClass a in arr) 
                {
                    if (a.Name == _name && a.CourseID == _courseid) 
                    {
                        return a.Score;
                    }
                }
                return -1;
            }
            set 
            {
                arr.Add(new IndexClass(_name, _courseid, value)); //arr["張三",1]=90
            }
        }
        //重載索引器
        public ArrayList this[string _name] 
        {
            get 
            {
                ArrayList temp = new ArrayList();
                foreach (IndexClass b in arr) 
                {
                    if (b.Name == _name) 
                    {
                        temp.Add(b);
                    }
                }
                return temp;
            }
        }
    }
}
發佈了21 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章