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:
- 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'.
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
{
}