All right, after we discussed the "merit" of asking such questions (:-)), I can share something more essential, just for example.
Imagine you have a huge file of some objects. The size of the file could be more than available memory or even addressable space (it would be quite possible in 32-bit systems). You can represent this file as the collection of objects accessible by index. Here is how:
class FileArray {
string this[ulong index] {
get {
ulong position =
int recordLength =
string value =
return value;
}
}
}
This way, you can hide a pretty complex algorithm of dealing with some big file behind a simple indexed interface, the only thing visible from outside of this class. If you will, you can consider it as one of the variants of the
facade pattern:
http://en.wikipedia.org/wiki/Facade_pattern[
^].
One more interesting topics: some think that the limitation of indexers is having only one per type. This is sometimes considered as the argument for having indexed properties which are not available in .NET. I would like to have indexed properties in .NET as well (please see my comments to Solution 1), but this limitation of indexers is not the case in .NET: the workaround is easy. Please see my past answer:
Location of a refernced object in an array?[
^].
And I see no "demerits" in indexers, I only see some "demerit" in absence of indexed properties, which are explained in the link provided in Solution 1.
—SA