C#C
C#7mo ago
Faker

✅ How does an indexer work and why is it important?

Hello, was just reading a bit about indexers. From what I've understood, an indexer allows our class to behave like an array.

My questions are:

  1. When we say that our class behave as a array, this mean that each instance created represent an index? Like MyClass[0] represents the first instance?
  2. What does the this keyword means in the indexer syntax?
C#
public T this[int i]
{
    get => arr[i];
    set => arr[i] = value;
}

  1. In order to use an indexer, we must always have one of the attribute declared as a 1D array?
  2. Why are indexers important and helpful
Was this page helpful?