The way I look at indexers is that (right or wrong!), Accessing something by index should be more efficient than accessing it in any other way, because in some way a form or form, class, whose index I use stores some form of index, which allows it to quickly look up values ​​when accessing this path.
A classic example is an array, when you access the element n of an array using the myarray [3] code, the compiler / interpreter knows how large (in memory) the elements of the array are and can consider it as an offset from the beginning of the array. You could also "for(int i = 0; i < myarray.length; i++) { if (i = 3) then { .. do stuff } }" (not that you would ever want!) That would be less effective. It also shows how an array is a bad example.
Let's say you had a collection class that stores, mmm, DVDs, so:
public class DVDCollection { private Dictionary<string, DVD> store = null; private Dictionary<ProductId, string> dvdsByProductId = null; public DVDCollection() {
It’s just my 2p, but, as I said, I always considered the “indexer” a reasonable way to get data from something.
Rob 02 Feb 2018-10-02T00 : 00Z
source share