可能重复:
Should I use public properties and private fields or public fields for data?
C#: Public Fields versus Automatic Properties
我有以下代码:
namespace Test {
class My1 {
private int intMark; //private
public int intAge;
public int Mark {
get { return intMark; }
set { intMark = value; }
}
}
class mainClass {
static void Main() {
My1 obj = new My1();
obj.Mark = 100;
obj.intAge = 34;
// what is the difference between these statements..
}
}
}
在上面的例子中,我们可以将intMark声明为公共变量,然后我们可以处理该对象。而不是为什么属性?