我看到很多代码使用自动生成的属性,例如{get; private set;}
或{get; protected set;}
。
这种private
或protected
设置有什么优势呢?
我尝试了这段代码,但当我使用Foo{get; set;}
时效果相同。
public class MyClass
{
public int Foo {get; private set;}
public static void RunSnippet()
{
var x = new MyClass();
x.Foo = 30;
Console.WriteLine(x.Foo);
}
...
}