Skip to content

C# Feature – Indexers

Indexers let instances be indexed like arrays, e.g., obj[key].

C# 1 (2002)

public string GetByKey(string key) { /* ... */ }
public string this[string key] { get => Lookup(key); set => Store(key, value); }
public class DictLike
{
private readonly Dictionary<string,string> _m = new();
public string this[string k] { get => _m[k]; set => _m[k] = value; }
}
  • Use clear argument names/types; document behaviors for missing keys.
  • Works with spans/index/range (C# 8+) on indexer types.