English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
インデクサは、クラスや構造体の内部集合を配列のようにアクセスできる特別な属性です。C#では、カスタムインデクサ、汎用インデクサ、オーバーロードインデクサを定義できます。
thisキーワードと括弧[]を使用した属性でインデクサを定義できます。
構文
<return type> this[<parameter type> index] { get{ // 内部集合の指定インデックスから値を返す } set{ // 内部集合の指定インデックスに値を設定 } }
以下の例では、クラス内にインデクサを定義しています。
class StringDataStore { private string[] strArr = new string[10; // 内部数据存储 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クラスは、strArrにインデクサを定義しています。今では、StringDataStoreからstrArrを使用して文字列値を追加や取得することができます。
StringDataStore strStore = new StringDataStore(); strStore[0] = "One"; strStore[1] = "Two"; strStore[2] = "Three"; strStore[3] = "Four"; for (int i = 0; 10 ; i++) Console.WriteLine(strStore[i]);
One Two Three Four
C#から 7最初は、get と set に対して式体文法を使用できます。
class StringDataStore { private string[] strArr = new string[10; // 内部数据存储 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; } } }
上面的泛型索引器可以与任何数据类型一起使用。以下示例演示了泛型索引器的用法。
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]);
可以用不同的数据类型重载索引。以下示例使用int类型索引和string类型索引重载索引器。
class StringDataStore { private string[] strArr = new string[10; // 内部数据存储 // 整型索引器 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引数を許可しません。