14

Is there any way to implement an interface explicitly using an automatic property? For instance, consider this code:

namespace AutoProperties
{
    interface IMyInterface
    {
        bool MyBoolOnlyGet { get; }
    }

    class MyClass : IMyInterface
    {
        static void Main(){}

        public bool MyBoolOnlyGet { get; private set; } // line 1
        //bool IMyInterface.MyBoolOnlyGet { get; private set; } // line 2
    }
}

This code compiles. However, if you replace line 1 with line 2 it does not compile.

(It's not that I need to get line 2 working - I'm just curious.)

skaffman
  • 398,947
  • 96
  • 818
  • 769
user181813
  • 1,861
  • 6
  • 24
  • 42
  • 3
    For "why" - ask yourself... how would I assign it? – Marc Gravell Oct 11 '10 at 09:44
  • I get two errors: 1. 'AutoProperties.MyClass.AutoProperties.IMyInterface.MyBoolOnlyGet.set' adds an accessor not found in interface member 'AutoProperties.IMyInterface.MyBoolOnlyGet'. 2. The modifier 'private' is not valid for this item – user181813 Oct 12 '10 at 05:37

2 Answers2

15

Indeed, that particular arrangement (explicit implementation of a get-only interface property by an automatically implemented property) isn't supported by the language. So either do it manually (with a field), or write a private auto-implemented prop, and proxy to it. But to be honest, by the time you've done that you might as well have used a field...

private bool MyBool { get;set;}
bool IMyInterface.MyBoolOnlyGet { get {return MyBool;} }

or:

private bool myBool;
bool IMyInterface.MyBoolOnlyGet { get {return myBool;} }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
5

The problem is that the interface has only getter and you try to explicitly implement it with getter and setter.
When you explicitly implement an interface the explicit implementation will be called only when your reference if of the interface type, so... if the interface only has getter there is no way to use the setter so it makes no sense to have a setter there.

This, for example will compile:

namespace AutoProperties
    {
        interface IMyInterface
        {
            bool MyBoolOnlyGet { get; set; }
        }

        class MyClass : IMyInterface
        {
            static void Main() { }

            bool IMyInterface.MyBoolOnlyGet { get; set; } 
        }
    }
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
  • I don’t see this explaining why explicitly implementing the interface should be any more restricted than implicit implementation. Maybe it makes sense if implicit implementation results in the C# compiler automatically generating the necessary explicit implementation code for you. Could you bridge the logical gap here—what exactly makes implicit different from explicit that breaks the OP’s code? – binki Jan 28 '15 at 21:28
  • Suppose you could have defined such private setter in an explicit implementation. How would you use it? 'this.setter = ...' will delegate to the implicit setter (because this is of type MyClass) and '((IMyInterface) this).setter' will fail because IMyInterface does not have the setter defined. – Itay Karo Jan 29 '15 at 04:48
  • That’s the whole point. If the explicitly implemented property could be auto implemented, then I wouldn’t have to manually implement my own backing store for it. I could also rely on the interface only defining a getter to hide my private setter. Well, there are problems with that I guess, but in my mind it is basically the same problem as being [unable to override just a property’s setter or getter](http://stackoverflow.com/a/6058526/429091). You should be able to tell the compiler that just your setter or just your getter is implementing the explicit interface. – binki Jan 29 '15 at 15:00