C # - Indexer in its meaningful application

I understand that indexer allows us to access the collection inside the class, as if the class itself were an array .

Suppose we are developing a project where we come to the real practical use (an example can help) of such indexers?

+7
c # language-features
Nov 19 '09 at 17:09
source share
6 answers

Look at almost any of the collection classes in the framework for an example. If I wanted to get an element from a hash table in a language without an indexer, I would use syntax, for example:

object o = table.getvalue(key); 

where the indexer allows you to simplify the syntax somewhat:

 object o = table[key]; 

(intentionally ignoring typing / generic issues in the above samples)

+8
Nov 19 '09 at 17:14
source share

You have some examples in the general collections of the structure itself. The dictionary is a pretty good example:

 Dictionary<string, User> Users; Users["Jorge"].ResetPassword(); 

It would be very elegant and simple than

 Users.GetElementByKey("Jorge").ResetPassword(); 
+5
Nov 19 '09 at 17:14
source share
Indexer

allows us to access the collection in the class, as if the class itself was an array.

While this is true, you can write a class in which it does not have a constant set of elements inside itself, but it can still dynamically provide you with the calculated value when indexing. Data can be collected in real time from the Internet or any other data source - I imagine, for example, the SearchResults class, which I could use to get Google search results for a line like this: Console.out.WriteLine(SearchResults["Stackoverflow"].count) . It only collects data when I ask for it and return a collection of extracted elements.

+3
Nov 19 '09 at 17:41
source share

Suppose you are developing the Microsoft.NET Framework for use by other developers (which is really a "real project"). You need to write a class that acts like an array but supports dynamic resizing ( List<T> class). Without an indexer, you can write two methods for getting and setting items:

 public T get(int index) { ... } public void set(int index, T value) { ... } // (Java advocates: no offense intended) 

Suppose your library user wants to increment the second item. She will need to write:

 list.set(1, list.get(1) + 1); 

With an index, the syntax is much simpler, more readable, and clearer:

 list[1]++; 

(similar arguments apply to things like Dictionary<TKey,TValue> ...)

+2
Nov 19 '09 at 17:15
source share

Well, the obvious application for using indexers is ... (surprise) Collections.

For example:

  • Lists:

     list[4] 
  • Hash Tables:

     table["foo"] table[Color.Black] 

Remember that indexers are not limited to integer arguments.

+1
Nov 19 '09 at 17:14
source share

If the collection is sorted, then the first and last items can be useful to get the minimum or maximum item.

There are several objects in ASP.Net that use indexes, such as Application, Session, and Request.

0
Nov 19 '09 at 17:16
source share



All Articles