什么是C#中的Property Accessor递归? [英] What is Property Accessor Recursion in C#?

查看:102
本文介绍了什么是C#中的Property Accessor递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

c#属性设置程序主体,而无需声明类级属性变量

推荐答案

如果您想到吸气剂和设置器作为方法(它们实际上是背景中的方法-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);
}

您会注意到,两种情况都会导致无限递归,并以a结尾堆栈溢出

You will notice that both cases will result in infinite recursion that will end with a stack overflow.

所以不要t Do That™

So Don't Do That™

这篇关于什么是C#中的Property Accessor递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆