什么是 C# 中的属性访问器递归? [英] What is Property Accessor Recursion in C#?
本文介绍了什么是 C# 中的属性访问器递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是 C# 中的属性访问器递归?我看到有关如何解决它的文章,但想要它是什么的纯技术定义.
What is Property Accessor Recursion in C#? I see articles of how to resolve it, but want a pure technical definition of what it is.
有关如何解决此问题的资源:
Resources on how to resolve it:
推荐答案
如果您将 getter 和 setter 视为方法(它们确实是后台的方法 - C# 只是对您隐藏了这一点),这会变得更加清晰.
It becomes clearer if you think of the getters and setters as methods (they really are methods in the background - C# just hides that from you).
- 无论何时检索属性的值,都是在调用 get 方法
- 每当你设置一个属性的值时,你就是在调用 set 方法
因此,如果您有一个如下所示的属性:
So if you have a property that looks like this:
public string MyProperty {
get {
return this.MyProperty;
}
set {
this.MyProperty = value;
}
}
这真的很像拥有这两种方法:
That is really like having these two methods:
string get_MyProperty() {
return get_MyProperty();
}
void set_MyProperty(string value) {
set_MyProperty(value);
}
您会注意到这两种情况都会导致无限递归,并以堆栈溢出结束.
You will notice that both cases will result in infinite recursion that will end with a stack overflow.
所以不要那样做
这篇关于什么是 C# 中的属性访问器递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文