【发布时间】:2016-09-26 13:49:32
【问题描述】:
readonly 修饰符和 get-only 属性之间有什么区别吗?
例子:
public class GetOnly
{
public string MyProp { get; }
}
public class ReadOnly
{
public readonly string MyProp;
}
奖励:有没有办法制作一个同时适用于两者的界面? (与泛型一起使用)
public interface ISomething
{
public string MyProp { get; }
}
public class GetOnly : ISomething
{
public string MyProp { get; }
}
public class ReadOnly : ISomething // Cannot implement
{
public readonly string MyProp;
}
非常感谢!
【问题讨论】:
-
您可以通过显式实现在 ReadOnly 类上实现接口
标签: c# properties