C# Feature β Indexers
Overview
Section titled βOverviewβIndexers let instances be indexed like arrays, e.g., obj[key].
Introduced In
Section titled βIntroduced InβC# 1 (2002)
public string GetByKey(string key) { /* ... */ }public string this[string key] { get => Lookup(key); set => Store(key, value); }Annotated Example
Section titled βAnnotated Exampleβpublic class DictLike{ private readonly Dictionary<string,string> _m = new(); public string this[string k] { get => _m[k]; set => _m[k] = value; }}Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Use clear argument names/types; document behaviors for missing keys.
Related Features
Section titled βRelated FeaturesβVersion Notes
Section titled βVersion Notesβ- Works with spans/index/range (C# 8+) on indexer types.