Indekser
Indekser (ang. Indexer) – element języka programowania C#, który pozwala indeksować instancje danej klasy lub struktury tak jak tablice. Indeksery są podobne do właściwości klasy, z tą różnicą, że ich metody dostępowe mogą przyjmować parametry. Składnia indeksera wygląda następująco:
<modyfikator_dostępu> <zwracany_typ> this [lista_argumentów] { get { // implementacja } set { // implementacja } }
Przykład
using System; class IndexableClass { private int[] array = new int[10]; public int this[int i] { get { return array[i]; } set { array[i] = value; // słowo kluczowe value jest używane w odniesieniu do wartości przekazanej do indeksera set } } } class Program { static void Main(string[] args) { IndexableClass indexableClass = new IndexableClass(); indexableClass[0] = 13; Console.WriteLine(indexableClass[0].ToString()); // wyświetli na konsoli 13 } }
Linki zewnętrzne
- Więcej informacji o indekserach (ang.)