English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Un indice è una proprietà speciale che consente di accedere a un oggetto o struttura come se fosse un array interno alla raccolta. C# ci permette di definire indici personalizzati, indici generici e indici sovraccaricati.
È possibile definire indici utilizzando proprietà con la parola chiave this e parentesi quadre [].
Sintassi
<return type> this[<parameter type> index] { get {}} get {}} {} // Restituisce il valore all'indice specificato dell'insieme interno set { {} {}
Esempio: Definizione dell'indice
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 sopra definita ha definito un indice per l'array privato strArr. Ora è possibile utilizzare StringDataStore per aggiungere e recuperare valori di stringa come se fosse un array, come illustrato di seguito.
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]);
Uno Due Tre Quattro
A partire dalla versione C# 7, è possibile utilizzare la sintassi delle espressioni corporee per get e set.
class StringDataStore { private string[] strArr = new string[10]; // 内部数据存储 public string this[int index] { get => strArr[index]; set => strArr[index] = value; {} {}
L'indice può anche essere generico. Di seguito è riportato un esempio di classe generica che include un indice generico.
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; {} {} {}
L'indice generico può essere utilizzato con qualsiasi tipo di dati. Di seguito è riportato un esempio di utilizzo dell'indice generico.
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"]); {} {}
Attenzione: gli indici non permettono parametri ref e out.