I have a collection class MyCollection<T>.
I have implemented T this[string name], which works fine.
I tried to also implement T Item(string name) but of course that gave an error:
The type
MyCollection<T>already contains a definition for Item
as it should. However I also want to overload Item by implementing a function T Item(string, bool) but this also throws up the same error.
Why would the compiler think that T Item(string, bool) clashes with the indexer T Item(string)? If it weren’t an indexer but just an ordinary method these two declarations would be two perfectly valid overloads.
You can absolutely overload an indexer – but you can’t overload it with a method called
Item. To overload the indexer, you just need:EDIT: Okay, so it looks like:
Item, which is slightly surprising as they’re both properties under the hoodI suspect that you can have indexers alongside properties called
Itemif you change the generated name of the indexer’s property usingDefaultMemberAttribute, but I haven’t tried it.