[英]Declare property getter in parent interface and setter in child interface in c#
我想声明的父接口和子接口的setter一个属性的getter:
public interface IReadOnlyValue
{
int Value { get; }
}
public interface IValue : IReadOnlyValue
{
int Value { set; }
}
public class Value : IValue
{
int Value { get; set; }
}
这不会编译,因为IValue
的Value
hides
了IReadOnlyValue
那个。 知道我需要Value
才能成为财产 ,有没有办法做到这一点?
看来这是一个类名 Value
错了:
错误CS0542“值”: 成员名称不能与它们的封闭类型 相同
(大胆是我的)。 如果将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.