索引器是一種特殊的屬性,它允許像訪問其內(nèi)部集合的數(shù)組一樣訪問類或結(jié)構(gòu)。C#允許我們定義自定義索引器,通用索引器以及重載索引器。
可以使用帶有 this 關鍵字和方括號 [] 的屬性來定義索引器。
語法
<return type> this[<parameter type> index] { get{ // 從內(nèi)部集合的指定索引返回值 } set{ // 在內(nèi)部集合中的指定索引處設置值 } }
下面的示例在類中定義一個索引器。
class StringDataStore { private string[] strArr = new string[10]; // 內(nèi)部數(shù)據(jù)存儲 public string this[int index] { get { if (index < 0 || index >= strArr.Length) throw new IndexOutOfRangeException("Index out of range"); return strArr[index]; } set { if (index < 0 || index >= strArr.Length) throw new IndexOutOfRangeException("Index out of range"); strArr[index] = value; } } }
上面的 StringDataStore 類為其私有數(shù)組strArr定義了一個索引器?,F(xiàn)在,您可以像使用數(shù)組一樣使用 StringDataStore 從 strArr 添加和檢索字符串值,如下所示。
StringDataStore strStore = new StringDataStore(); strStore[0] = "One"; strStore[1] = "Two"; strStore[2] = "Three"; strStore[3] = "Four"; for(int i = 0; i < 10 ; i++) Console.WriteLine(strStore[i]);
One Two Three Four
從 C# 7開始,可以對 get 和 set 使用表達式體語法。
class StringDataStore { private string[] strArr = new string[10]; // 內(nèi)部數(shù)據(jù)存儲 public string this[int index] { get => strArr[index]; set => strArr[index] = value; } }
索引器也可以是泛型。 下面的泛型類包括泛型索引器。
class DataStore<T> { private T[] store; public DataStore() { store = new T[10]; } public DataStore(int length) { store = new T[length]; } public T this[int index] { get { if (index < 0 && index >= store.Length) throw new IndexOutOfRangeException("Index out of range"); return store[index]; } set { if (index < 0 || index >= store.Length) throw new IndexOutOfRangeException("Index out of range"); store[index] = value; } } public int Length { get { return store.Length; } } }
上面的泛型索引器可以與任何數(shù)據(jù)類型一起使用。以下示例演示了泛型索引器的用法。
DataStore<int> grades = new DataStore<int>(); grades[0] = 100; grades[1] = 25; grades[2] = 34; grades[3] = 42; grades[4] = 12; grades[5] = 18; grades[6] = 2; grades[7] = 95; grades[8] = 75; grades[9] = 53; for (int i = 0; i < grades.Length;i++) Console.WriteLine(grades[i]); DataStore<string> names = new DataStore<string>(5); names[0] = "Steve"; names[1] = "Bill"; names[2] = "James"; names[3] = "Ram"; names[4] = "Andy"; for (int i = 0; i < names.Length;i++) Console.WriteLine(names[i]);
可以用不同的數(shù)據(jù)類型重載索引。下面的示例使用int類型索引和string類型索引重載索引器。
class StringDataStore { private string[] strArr = new string[10]; // 內(nèi)部數(shù)據(jù)存儲 // 整型索引器 public string this[int index] { get { if (index < 0 || index >= strArr.Length) throw new IndexOutOfRangeException("Index out of range"); return strArr[index]; } set { if (index < 0 || index >= strArr.Length) throw new IndexOutOfRangeException("Index out of range"); strArr[index] = value; } } // 字符串類型索引器 public string this[string name] { get { foreach (string str in strArr){ if(str.ToLower() == name.ToLower()) return str; } return null; } } } class Program { static void Main(string[] args) { StringDataStore strStore = new StringDataStore(); strStore[0] = "One"; strStore[1] = "Two"; strStore[2] = "Three"; strStore[3] = "Four"; Console.WriteLine(strStore["one"]); Console.WriteLine(strStore["two"]); Console.WriteLine(strStore["Three"]); Console.WriteLine(strStore["Four"]); } }
注意:索引器不允許ref和out參數(shù)。