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) { ... }
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> ...)
Mehrdad Afshari Nov 19 '09 at 17:15 2009-11-19 17:15
source share