我想在父接口中声明属性的getter和子接口中的setter:
public interface IReadOnlyValue
{
int Value { get; }
}
public interface IValue : IReadOnlyValue
{
int Value { set; }
}
public class Value : IValue
{
int Value { get; set; }
}
这不会编译,因为来自IValue
hides
Value
来自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; }
}