Posts Tagged ‘attribute’

Name of the indexer property in C#

Wednesday, December 26th, 2007

Adding an indexer to a class that already owns a property named ‘Item’ makes the code impossible to compile. The compiler and Resharper both give the following error: The type ‘…’ already contains a definition for ‘Item’. Trying to disassemble a class with an indexer in Reflector shows the indexer is named ‘Item’ by default.

After a brief search I found that in VB.net you are capable of naming the indexer property and even this post claimed that in the past C# was also the same. However I found a workaround which lets you define a name for the indexer by assigning an attribute to it. Here is the attribute:


[System.Runtime.CompilerServices.IndexerName("MoreAppropriateName")]


Ilia