【发布时间】:2019-08-23 03:05:23
【问题描述】:
在创建不可变类型时,为只读字段创建属性是否有任何价值?
公共只读字段:
public class MyClass
{
public readonly string MyText;
public MyClass (string theText)
{
MyText = theText;
}
}
或
具有公共属性的私有只读字段:
public class MyClass
{
private readonly string myText;
public string MyText
{
get { return myText; }
}
public MyClass(string theText)
{
myText = theText;
}
}
【问题讨论】:
-
见here。我猜你想在第二个代码示例中返回 myText。
标签: c# properties field