Paweld_00
Paweld_00

Reputation: 81

Can I override properties in c#?

I have got in my class two properties :

public bool this[string i]
public double this[string i]

Then when i want to give them a values there is an error with distinguishing it.

 Class cl = new Class() ;
 cl["something"] = true; //error
 cl["something_else"] = 10.1; //error

Is there a way to not add an argument to one of the properties and just override it ?

Upvotes: 1

Views: 621

Answers (3)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391276

No, it is not possible to distinguish the properties or methods by return type only.

The rules for the signature of an indexer is specified in the C# specification in section "10.9 Indexers":

The formal parameter list of an indexer defines the signature (§3.6) of the indexer. Specifically, the signature of an indexer consists of the number and types of its formal parameters. The element type and names of the formal parameters are not part of an indexer’s signature.
The signature of an indexer must differ from the signatures of all other indexers declared in the same class.

(my emphasis)

From "3.6 Signatures and overloading":

The signature of a method consists of the name of the method, the number of type parameters, and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter of the method that occurs in the type of a formal parameter is identified not by its name, but rather by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the params modifier that may be specified for the rightmost parameter, or the optional type parameter constraints.

(again, my emphasis)

Also, at the bottom of the same chapter:

Also, note that the return type and the params modifier are not part of a signature, so it is not possible to overload solely based on return type or on the inclusion or exclusion of the params modifier.

(again, my emphasis)

The only way to have multiple indexers on the same class is to give them different parameter types or different number of parameters.

My advice would be to do one of the following:

  • Switch to using named properties, it isn't entirely clear what you get with just indexers if you have more than one
  • Encapsulate the indexers into different objects so that you would write instance.Percentages["name"] and instance.Flags["name"]. You cannot name indexers in C#, but you can expose them using another object returned from a property.

Upvotes: 2

dcastro
dcastro

Reputation: 68640

The problem is not in the usage, it's in the declaration. Seeing as return types do not take part in overload resolution, those two indexers (not properties) have the same signature, and therefore the program won't compile.

From section "3.6 Signatures and overload" of the C# Specification:

The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter of the method that occurs in the type of a formal parameter is identified not by its name, but by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the params modifier that may be specified for the right-most parameter, nor the optional type parameter constraints.

You may have to fallback to using plain old getters/setters (e.g.: bool GetStatus(string i) double GetPercentage(string i), or rethink your design.

Upvotes: 2

anion
anion

Reputation: 2049

this is no valid c# Syntax. take a look at this page for an Explanation how to override c# properties: https://stackoverflow.com/a/8447846/3905529

Upvotes: 0

Related Questions