Search code examples
c#explicit-interface

Can one reference a same-named implicit property in an explicit Interface implementation?


Say I have a type that implements a property with a string type:

public class Record
{
     public string Value { get; set; }
}

Then I have an interface that defines a property with the same name:

public interface IIntValued
{
     public int Value { get; set; }
}

I can use explicit interface as follows:

public class Record : IIntValued
{
     public string Value { get; set; }
     int IIntValued.Value 
     {
          get{ return 0; } set{}
     }
}

However, if I want to be able to reference the string "Value" in my explicit interface, can I do it? If so, how? I imagine it to be something like:

public class Record : IIntValued
{
     public string Value { get; set; }
     public int IIntValued.Value 
     {
          get
          {
               string value = /*Magic here*/.Value;
               return int.parse(value); 
          } 
          set{}
     }
}

As you can see, I want the "string valued" "Value" property for an expression in the "int valued" "Value" property. If it were another explicitly implemented interface member, I could typecast to that Interface and then use, but how would it work for an implicit type member?

Note: The example is a bit contrived, but hopefully demonstrates the language question.


Solution

  • For an implicit type member, just Value or this.Value should be fine - because it won't resolve to IIntValued.Value by default.