Click here to Skip to main content
15,898,987 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys, Ive come accross a new concept in C# that I am having a hard time finding a use for - Indexers. I know that an indexer is a way to index classes like arrays, but what is the advantage of an indexer over a generic (or not) list of classes?

Could someone elaborate on some uses/applications of an indexer?
Posted

1 solution

One word: indexer is more abstract. There are many cases when there is no any container behind — the property is calculated on the fly. I widely use this feature; you can consider it as a neat syntax candy, compared to something like "FindElementByName" or "GetElementByIndex".

[EDIT]
You can find interesting application in my code sample in one of my answers here:
Location of a refernced object in an array?[^].

—SA
 
Share this answer
 
v3
Comments
Isaiah83 17-May-11 22:03pm    
Clarify what you mean by container, please. Do you mean there isn't a specific collection like List/ArrayList/Array to define the collection?
Sergey Alexandrovich Kryukov 17-May-11 22:29pm    
Yes, but anything that can be indexed with "[]". With indexers, the index type can be anything.
Please see my code sample; I just added a reference for you. Ask another question is it's not clear.
--SA
Isaiah83 17-May-11 23:00pm    
Now I can see the power of indexers. It seems that being able to index with anything (datatype, user defined etc..) is really desireable. I need more time to decipher your example, but thank you for sharing as it did give me some insight. 5/5. Cheers
Sergey Alexandrovich Kryukov 17-May-11 23:05pm    
Great! You're welcome.
Thanks for accepting this answer. Your follow-up questions are welcome.

Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900