+ 3

Indexers Questions

I just finished the Indexer's chapter and I have some questions: a) What is the advantage of Indexers over a normal array? The only thing I see is how you get to the value: object[2] instead of object.arr [2]. b) Can you have more than an indexer for an object of a class? For example one for int type and another for char type? Thanks for taking your time to read this.

12th Jan 2017, 8:11 AM
Andres G
Andres G - avatar
2 Antworten
+ 5
indexer is basically a feature so that when you make your own classes then their objects could have a functionality like this object[ ]. Suppose you make your own array class or any other class and want to access its elements using the [ ] operator then you must have written an indexer in that class which will tell what happens when [ ] operator is used with the objects of that class.In short indexers and array are not be compared. indexer is just a feature you give to your class For your second question. you must learn generics to find a way around for multiple indexers otherwise you cant have more than one indexer.
12th Jan 2017, 4:34 PM
Ahmed Jamil
0
good luck
27th Jan 2017, 4:19 PM
Idan Moral
Idan Moral - avatar