Is named indexer property possible?
No - you can't write named indexers in C#. As of C# 4 you can consume them for COM objects, but you can't write them.
As you've noticed, however, foo.Bars[index]
will do what you want anyway... this answer was mostly for the sake of future readers.
To elaborate: exposing a Bars
property of some type that has an indexer achieves what you want, but you should consider how to expose it:
- Do you want callers to be able to replace the collection with a different collection? (If not, make it a read-only property.)
- Do you want callers to be able to modify the collection? If so, how? Just replacing items, or adding/removing them? Do you need any control over that? The answers to those questions would determine what type you want to expose - potentially a read-only collection, or a custom collection with extra validation.