Hello.
In this video, we're going to talk about Question 28.
What are indexers?
Indexers are something we use all the time, usually without giving it much thought.
Whenever accessing an element under a specific index of our east, we are actually calling this index
are.
This looks exactly the same as accessing the first element of an array.
We fleeced using the indexer looks extremely simple, but there's a lot going on in the last class whenever
we use its indexer.
Let's take a look into its source code.
We can recognize that this is the definition of the index, her by this part, that this keyword and
the parameter in brackets for the case of the least.
The index is quite complex, but in the end its job is to get or set the value of the internal error.
That's the least holds.
We learned about it in the what is at least lecture indexers don't necessarily use integers as barometers.
For example, when using dictionaries, we use indexers to access another month under a given key and
the key of the dictionary can be whatever we want.
For example, if the key of the dictionary is a string, then the parameter of the index R will also
be a string.
We can define our own indexers in the types we create.
Let's define a simple class that works as a wrapper for our.
For now, when trying to use an indexer on an object of this class, we get a compilation error because
this class does not support it to make it work in the mildest class.
We must define an index are accepting and.
On Get, this index simply retrieves the value from the array and onset it overwrites it with the provided
value.
As we can see, this gold now compiles.
We can define Usman indexers as we want if they only differ by the types or account of parameters.
For example, I can add an index are accepting a string to the my wrist glass.
I could use this indexer like this.
Indexers with multiple parameters are also allowed.
Please note that it is also possible to have an indexer, we've got there only or we sat there only,
but this is more unusual.
In this case, will be able to access an element of a given index, but we won't be able to overwrite
it.
As you can see, accessing the element works, but certainly it does not, because the index are taking
two parameters.
It does not have us later.
Let's sum up indexers allow instances of a type to be indexed.
Just like all, index cards are most often used with types representing corrections, but we can add
them to any type.
Indexes are simple and not too are to use for developers, and we should consider adding them to our
types, especially if we already have some methods like good value at index.
In this case, we should definitely consider refactoring and introducing an index.
During the interview, you can be asked if it's possible to have a class with an index are accepting
a string as a parameter.
Well, of course it is.
We can define index cards within the parameters.
And can we have more than one index are defined in the class?
Yes.
Just like with method overloading, we can have as many indexes as we want, as long as they differ
by the type count or order of parameters.
All right, that's it about indexers.
Thanks for watching and see you in the next video.