根據這篇的解釋
如果這麼寫就會 Stack overflow
private int _salary
public int salary
{
get
{
return _salary;
}
set
{
salary = value;
}
}
原因是salary = value
會呼叫 salary 的 set method
而 set method 裡又執行salary = value
結果就是無窮迴圈
然後就爆了 stack overflow
正確寫法應該是
set : 將 value 指定給 private attribute _salary
get : return private attribute _salary
private int _salary
public int salary
{
get
{
return _salary;
}
set
{
_salary = value;
}
}