C# 为什么要为属性创建私有字段?
我一直在比较属性和字段的例子,有一件事我总是不加解释地看到,那就是创建一个私有字段,然后创建一个属性。为什么我们需要C# 为什么要为属性创建私有字段?,c#,properties,c#-6.0,C#,Properties,C# 6.0,我一直在比较属性和字段的例子,有一件事我总是不加解释地看到,那就是创建一个私有字段,然后创建一个属性。为什么我们需要\u foo,而不能像下面那样运行代码,或者用foo=value替换foo=foo 当我运行它时,我看到它创建了一个堆栈溢出异常,所以我猜它正在寻找一个值来填充Foo或value,但我认为当我执行def.Foo=55时会处理这个问题 即使创建私有字段是最佳实践,我仍然希望更好地了解导致溢出的实际原因 class ABC { private int _foo; //Why c
\u foo
,而不能像下面那样运行代码,或者用foo=value
替换foo=foo
当我运行它时,我看到它创建了一个堆栈溢出异常,所以我猜它正在寻找一个值来填充Foo
或value
,但我认为当我执行def.Foo=55
时会处理这个问题
即使创建私有字段是最佳实践,我仍然希望更好地了解导致溢出的实际原因
class ABC
{
private int _foo; //Why create this?
public int Foo
{
get { return Foo; }
set
{
Foo = Foo;//more logic would go here. }
}
}
}
class Program
{
static void Main(string[] args)
{
ABC def = new ABC();
def.Foo = 55;
Console.WriteLine($"The value of Foo is {def.Foo}.");
Console.ReadLine();
}
}
这是因为字段和属性不相同
private int _foo; //field
public int Foo //property
{
get { return _foo; } //should be _foo
set
{
_foo = value;
}
}
通常,您使用公共属性来访问私有字段,因为可能希望执行分配/获取字段值以外的其他操作
例如:
private bool selectedIndexChanged; //note this additional field
public bool SelectedIndexChanged {
get { return selectedIndexChanged; }
set { selectedIndexChanged = value; }
}
private int selectedIndex; //Why create this?
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndexChanged = value != selectedIndex; //you update selectedIndexChanged here!
selectedIndex = value;
}
}
private int _score;
public int Score
{
get { return _score; }
set
{
if(value > 100)
throw new Exception("Score cannot be more than 100");
_score = value;
}
}
请注意,您不直接访问私有字段,而是通过公共属性访问它们。拥有属性的好处在于,您不仅可以读取/分配字段值,还可以执行其他操作:检查输入的有效性、分配其他字段、调用事件等
即使创建私有字段是最佳实践,我仍然会
想更好地了解到底是什么导致了
溢出
溢出不是由最佳实践引起的,而是由错误实践引起的:
private int _foo; //Why create this?
public int Foo
{
get { return Foo; } //this should not happen, wrong practice
set
{
Foo = Foo; //neither should this happen, another wrong practice
}
}
上述代码中发生的情况是:
因此导致溢出…这是因为字段和属性不相同
private int _foo; //field
public int Foo //property
{
get { return _foo; } //should be _foo
set
{
_foo = value;
}
}
通常,您使用公共属性来访问私有字段,因为可能希望执行分配/获取字段值以外的其他操作
例如:
private bool selectedIndexChanged; //note this additional field
public bool SelectedIndexChanged {
get { return selectedIndexChanged; }
set { selectedIndexChanged = value; }
}
private int selectedIndex; //Why create this?
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndexChanged = value != selectedIndex; //you update selectedIndexChanged here!
selectedIndex = value;
}
}
private int _score;
public int Score
{
get { return _score; }
set
{
if(value > 100)
throw new Exception("Score cannot be more than 100");
_score = value;
}
}
请注意,您不直接访问私有字段,而是通过公共属性访问它们。拥有属性的好处在于,您不仅可以读取/分配字段值,还可以执行其他操作:检查输入的有效性、分配其他字段、调用事件等
即使创建私有字段是最佳实践,我仍然会
想更好地了解到底是什么导致了
溢出
溢出不是由最佳实践引起的,而是由错误实践引起的:
private int _foo; //Why create this?
public int Foo
{
get { return Foo; } //this should not happen, wrong practice
set
{
Foo = Foo; //neither should this happen, another wrong practice
}
}
上述代码中发生的情况是: