初学者问题:下面的解决方案有什么缺点吗?
public string SomeProperty { get; private set; }与C# public variable as writeable inside the class but readonly outside the class中的一样
因为我绝对觉得它比:
string someProperty;
public string SomeProperty{
get {
return someProperty;
}
} 但是为什么C#不能像这样
GetOnly string someProperty; 哪一个和上面的是一样的?这不是更容易读写吗?
发布于 2013-07-30 00:27:10
汽车产业的意义是什么?
public Foo FooProperty { get; }你如何将它的值设置为有意义的东西?
以下代码:
public Foo FooProperty { get; private set; }明确声明您可以在类外读取它,并且只能在类内设置它。
发布于 2013-07-30 00:31:46
公共只读字段可以工作,但您只能在类构造函数中修改它。
public readonly string SomeField;
public MyClass()
{
SomeField = "SomeString";
}https://stackoverflow.com/questions/17929169
复制相似问题