CSharp - What is Indexer

Introduction

Indexers is easier way to access data elements in a list or dictionary of values.

For example string class has an indexer that lets you access each of its char values via an int index:

string s = "hello";
Console.WriteLine (s[0]); // 'h'
Console.WriteLine (s[3]); // 'l'

Syntax

The syntax for using indexers is like using arrays.

The index argument(s) can be of any type(s).

Indexers have the same modifiers as properties.

Indexers can be called null-conditionally by inserting a question mark before the square bracket:

string s = null;
Console.WriteLine (s?[0]);  // Writes nothing; no error.