Vivek Bernard Vivek Bernard - 9 months ago 75
C# Question

Real world use cases for C# indexers?

I've seen lot of examples for c# Indexers, but in what way will it help me in real life situations.

I know the C# guru wouldn't have added this if it wasn't a serious feature, but i cant think of a real world situation (not the foo bar stuff) to use indexers.

Note:I realize a related question exists, but it doesn't help me much.

Rob Rob
Answer Source

The way I look at indexers is that (rightly or wrongly!), accessing something by index should be more efficient than accessing it any other way, because in some way, shape or form, the class whose indexer I'm using stores some form of index that allows it to quickly lookup values when accessed that way.

The classic example is an array, when you access element n of an array by using the code myarray[3], the compiler/interpreter knows how big (memory-wise) elements of the array are and can treat it as an offset from the start of the array. You could also "for(int i = 0; i < myarray.length; i++) { if (i = 3) then { .. do stuff } }" (not that you'd ever want to!), which would be less efficient. It also shows how an array is a bad example.

Say you had a collection class that stores, umm, DVDs, so:

public class DVDCollection
    private Dictionary<string, DVD> store = null;
    private Dictionary<ProductId, string> dvdsByProductId = null;

    public DVDCollection()
        // gets DVD data from somewhere and stores it *by* TITLE in "store"
        // stores a lookup set of DVD ProductId's and names in "dvdsByProductid"
        store = new Dictionary<string, DVD>();
        dvdsByProductId = new Dictionary<ProductId, string>();

    // Get the DVD concerned, using an index, by product Id
    public DVD this[ProductId index]  
       var title = dvdsByProductId[index];
       return store[title];

Just my 2p, but, like I said,.. I've always considered an "indexer" as being an expedient way of getting data out of something.