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.)

2
  • 3
    For "why" - ask yourself... how would I assign it? Commented Oct 11, 2010 at 9: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 Commented Oct 12, 2010 at 5:37

2 Answers 2

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;} }
Sign up to request clarification or add additional context in comments.

Comments

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; } 
        }
    }

3 Comments

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?
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.
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. You should be able to tell the compiler that just your setter or just your getter is implementing the explicit interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.