[英]Declare property getter in parent interface and setter in child interface in c#
I want to declare the getter of a property in a parent interface and the setter in the child interface: 我想声明的父接口和子接口的setter一个属性的getter:
public interface IReadOnlyValue
{
int Value { get; }
}
public interface IValue : IReadOnlyValue
{
int Value { set; }
}
public class Value : IValue
{
int Value { get; set; }
}
This doesn't compile because the Value
from IValue
hides
the one from IReadOnlyValue
. 这不会编译,因为IValue
的Value
hides
了IReadOnlyValue
那个。 Is there a way to make this work, knowing that I need Value
to be a property ? 知道我需要Value
才能成为财产 ,有没有办法做到这一点?
It seems that it's a class name Value
that's wrong: 看来这是一个类名 Value
错了:
Error CS0542 'Value': member names cannot be the same as their enclosing type 错误CS0542“值”: 成员名称不能与它们的封闭类型 相同
(bold is mine). (大胆是我的)。 If you rename Value
to, say, MyValue
it'll be OK: 如果将Value
重命名为MyValue
那就可以了:
public interface IReadOnlyValue {
int Value { get; }
}
// It seems that IValue should have both "get" and "set"
// See IList<T> and IReadOnlyList<T> as an example
// However, you can drop "get" if you want
public interface IValue {
int Value { get; set; }
}
public class MyValue: IReadOnlyValue, IValue {
public int Value { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.