I've read the answers for Class with indexer and property named "Item", but they do not explain why can I have a class with multiple indexers, all of them creating Item
property and get_Item
/set_Item
methods (of course working well, as they are different overloads), but I cannot have an explicit Item
property.
Consider the code:
namespace Test
{
class Program
{
static void Main(string[] args)
{
}
public int this[string val]
{
get
{
return 0;
}
set
{
}
}
public string this[int val] //this is valid
{
get
{
return string.Empty;
}
set
{
}
}
public int Item { get; set; } //this is not valid
}
}
For two indexers there are four methods created:
Int32 get_Item(String val)
Void set_Item(String val, Int32 value)
String get_Item(Int32 val)
Void set_Item(Int32 val, String value)
I'd expect my property to create
Int32 get_Item()
Void set_Item(Int32 value)
These overloads are generally acceptable, but somehow the compiler won't let me create such a property.
Please note that I don't need a way to rename the indexer etc, this is known - I need an explanation. This answer: https://stackoverflow.com/a/5110449/882200 doesn't explain why I can have multiple indexers.