Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real world use cases for C# indexers?

Tags:

c#

indexer

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.

like image 531
Vivek Bernard Avatar asked Feb 02 '10 15:02

Vivek Bernard


People also ask

Where is C used now a days?

C is used for systems programming (operating systems, device drivers, compilers, embedded applications, etc.). This goes back more than four decades when C was used to write the Unix operating system. C was also used to write the Linux operating system.

Is C still used in 2020?

C/C++ is still powering the world despite number of new high level programming languages. Most of the major software applications including Adobe, Google, Mozilla, Oracle are all written in C/C++. There is a complete article on list of best applications written in C/C++.


2 Answers

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.

like image 92
Rob Avatar answered Oct 31 '22 15:10

Rob


The most obvious examples, as mentioned by Skurmedel, are List<T> and Dictionary<TKey, TValue>. What would you prefer over:

List<string> list = new List<string> { "a", "b", "c" }; string value = list[1]; // This is using an indexer  Dictionary<string, string> dictionary = new Dictionary<string, string> {     { "foo", "bar" },     { "x", "y" } }; string value = dictionary["x"]; // This is using an indexer 

? Now it may be relatively rare for you need to write an indexer (usually when you're creating a collection-like class), but I suspect you use them reasonably frequently.

like image 45
Jon Skeet Avatar answered Oct 31 '22 14:10

Jon Skeet