English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Un indexeur est une propriété spéciale qui permet d'accéder à une classe ou à une structure comme si elle était un tableau interne. C# nous permet de définir des indexeurs personnalisés, des indexeurs génériques et des indexeurs surchargés.
Vous pouvez définir des indexeurs en utilisant des propriétés avec la clé this et des crochets [].
Syntaxe
<return type> this[<parameter type> index] { get { // Retourner une valeur à l'index spécifique du jeu interne } set { // Définir une valeur à l'index spécifique du jeu interne } }
L'exemple suivant définit un indexeur dans une classe.
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; } } }
La classe StringDataStore définie un indexeur pour son tableau privé strArr. Vous pouvez maintenant utiliser StringDataStore pour ajouter et récupérer des valeurs de chaînes de caractères, comme suit.
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]);
Un Deux Trois Quatre
À partir de C# 7Début, vous pouvez utiliser la syntaxe d'expression corporelle pour get et set.
class StringDataStore { private string[] strArr = new string[10; // 内部数据存储 public string this[int index] { get => strArr[index]; set => strArr[index] = value; } }
Les indexeurs peuvent également être génériques. Le générique suivant inclut un indexeur générique.
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"]); } }
Attention : les indexeurs ne permettent pas les paramètres ref et out.