我担心人们总是使用 getter-setter 模式:
public int MyVariable { get; private set; }
public void SomeFunction(){
MyVariable = 10;
}
据我了解,编译为:
private int myVariable;
public int GetMyVariable(){
return myVariable;
}
private void SetMyVariable(int value){
myVariable = value;
}
public void SomeFunction()
{
SetMyVariable(10);
}
如果经常使用它不会影响程序的性能吗?这样做不是更好吗:
private int myVariable;
public int MyVariable { get {return myVariable; } }
public void SomeFunction(){
myVariable = 10;
}