C# Reference ยท Classes and Object-Oriented Concepts
Indexers
Reference for C# indexers: declare this[] accessors, use several parameters, string keys, read-only indexers, index-from-end support and interface indexers.
An indexer lets an object be used with array syntax: obj[key]. It is declared like a property named this, but with a parameter list in square brackets. List<T>, Dictionary<TKey,TValue> and string all expose indexers.
You can overload indexers by parameter types, use more than one parameter (grid[row, col]), and make them read-only. Like a property, an indexer is implemented by get and set accessors, so it can validate input or compute results.
public class Playlist { private readonly List<string> _songs = new(); public string this[int index] { get => _songs[index]; set => _songs[index] = value; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public T this[int index] { get { ... } set { ... } } public T this[string key] => Lookup(key); // read-only, expression-bodied public T this[int row, int col] { get { ... } set { ... } } public T this[Index index] => ...; // explicit Index support (C# 8) public T this[Range range] => ...;
| Part | Description |
|---|---|
| Parameters | One or more, of any type; they cannot be ref or out. |
get / set |
Same rules as property accessors, including init and different access levels for each accessor. |
| Overloads | Allowed when the parameter lists differ. |
[IndexerName("Item")] |
Changes the metadata name (default Item), mainly for interop. |
Usage notes
- Indexers have no name, so they cannot be accessed as
obj.Item. They can be declared in interfaces (T this[int i] { get; }) and implemented explicitly. - Index-from-end for free: if your type has an accessible
LengthorCountand anintindexer,obj[^1]works without writing anIndexoverload. Ranges (obj[1..3]) work if the type has aSlice(int, int)method. - Choose the right exception. Out-of-range ints should throw
ArgumentOutOfRangeException; a missing key should throwKeyNotFoundException, or you should offerTryGetValueinstead. - Indexers are properties in disguise, so they may run code, be virtual, or be slow. Don't hide expensive work behind
[]. - Do not overuse: use an indexer only when the type is conceptually a collection or a keyed lookup. For other operations, a named method is clearer.
- Null-conditional access works:
obj?[0]returnsnullifobjis null. - Index initializers:
new Week { [2] = "Midweek" }sets values during creation. - Struct indexers returning by
ref: arefreturning indexer (ref int this[int i] => ref _data[i];) lets callers modify elements in place without copying.