Mike de Klerk
Mike de Klerk

Reputation: 12338

Why does interface A hide property from interface B

See the code below. Visual Studio 2017 tells me that IGetSet.Value hides property IGet.Value and I should add the new keyword if this is intended. I understand the concept of hiding a property with respect to classes, but when it comes to interfaces, I do not see the point of the message. Whether I add the new keyword or not doesn't differ in behavior of course.

I want this stacking of the 2 interfaces because I want objects, that implement the read-write interface, to be usable in methods that are only allowed to get the value.

Now my questions are:

  1. Is this the way to achieve what I want? I see the message of 'hiding' properties as a signal saying 'You are doing something here you probably don't want to, try to solve it differently'.
  2. How can one interface hide the property of another? Doesn't it depend on the implementation of the interfaces? So why the message?

    public interface IGetSet : IGet
    {
        new string Value { get; set; }
    }
    
    public interface IGet
    {
        string Value { get; }
    }
    
    public class Tester : IGetSet
    {
        public string Value { get; set; }
    }
    
    [Test]
    public void InterfaceTest()
    {
        IGetSet tester = new Tester();
        tester.Value = "My Value";
        IGet getteronly = tester;
        var value = getteronly.Value;
    }
    

If I try to solve it with the code below I get this compile error 'CS0229 Ambiguity between 'IGet.Value' and 'ISet.Value''

public interface ISet
{
    string Value { set; }
}

public interface IGet
{
    string Value { get; }
}

public interface IGetSet : IGet, ISet
{

}

Upvotes: 5

Views: 848

Answers (2)

Pablo notPicasso
Pablo notPicasso

Reputation: 3161

If both interfaces have the same properties you have to implement them Explicitly

public interface ISet
{
    string Value { set; }
}

public interface IGet
{
    string Value { get; }
}

public class GetSetClass : IGet, ISet
{
    public string Value { get; set; }
    string IGet.Value // Explicitly implementation
    {
        get { return Value; }
    }

    string ISet.Value // Explicitly implementation
    {
        set { Value = value; }
    }
}

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157098

Is this the way to achieve what I want?

Yes.

I see the message of 'hiding' properties as a signal saying 'You are doing something here you probably don't want to, try to solve it differently'.

No, it means you have to assess if this was intentional. Did you really intend to create a second definition of the property which overrules the first one? Yes, you do in this case, but sometimes you wouldn't. So that is why there is a warning asking you to explicitly tell what you want.

As a side note, you could actually end up with a different implementation of both properties when you use explicit interface implementation:

public class Tester : IGetSet
{
    string IGet.Value { get { return "A"; } }

    string IGetSet.Value { get { return "B"; } set { } }
}

See there is a catch to the warning?

Upvotes: 4

Related Questions