简体   繁体   English

什么是C#中的Property Accessor递归?

[英]What is Property Accessor Recursion in C#?

What is Property Accessor Recursion in C#? 什么是C#中的Property Accessor递归? I see articles of how to resolve it, but want a pure technical definition of what it is. 我看到了有关如何解决它的文章,但是想要一个纯粹的技术定义。

Resources on how to resolve it: 有关解决方法的资源:

c# property setter body without declaring a class-level property variable 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). 如果将getter和setter视为方法(它们实际上是背景中的方法-C#只是向您隐藏了这些方法),则变得更加清楚。

  • Whenever you retrieve the value of a property, you are calling the get method 每当检索属性的值时,就在调用get方法
  • Whenever you set the value of a property, you are calling the set method 每当您设置属性的值时,您就在调用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 . 您会注意到,这两种情况都会导致无限递归,并以堆栈溢出结束。

So Don't Do That™ 所以不要那样做™

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM