属性将值设置为自身时如何发生堆栈溢出异常 [英] How Stack overflow exception occurs when property sets value to Itself

查看:49
本文介绍了属性将值设置为自身时如何发生堆栈溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

堆栈溢出异常是当可用于执行堆栈的有限内存耗尽时抛出的.NET异常(错误).这几乎总是由无限递归导致,最终导致嵌套方法调用过多.

Stack overflow exception is the .NET exception (error) that is thrown when the limited memory available for the execution stack is exhausted. This is almost always caused by an infinite recursion, which ultimately results in too many nested method calls.

以下代码在尝试设置值时会抛出堆栈溢出异常.

The following code will throw a stack overflow exception when trying to set a value.

  public String Name
  {
      get{return Name;}
      set{Name = value;}
  }

我知道引用存储在堆栈中(这里是它的名称),对象存储在堆中(字符串对象).这个地方的内存使用情况如何?谁能告诉我幕后发生了什么(内部实现细节)?支持字段的必要性是什么?

I know the references are stored in stack(here its Name) and Objects are stored in Heap(String Object). How excessive memory usage is happening at this place? Can anybody tell me whats happening behind the scene(The internal implementation details)? What is the necessity of a backing field?

推荐答案

当你编写 getter 的代码 {return Name;} 时,你就是在递归.如何?当一些代码想要获取Name 的值时,你的getter 方法只是告诉它,你为什么不试试再问我一次?" 看到问题了吗?当您尝试获取一个值时,它会告诉您需要再次询问自己.然后再次.再来一次!

When you make the getter's code {return Name;}, you are recursing. How? When some code wants to get the value of Name, your getter method just tells it, "Why don't you try asking me again?" See a problem? When you try getting a value, it'll tell you that you need to ask itself again. And again. And again!

每次访问属性并调用getter时,都会向堆栈中添加一个新条目.由于这段代码只会告诉其他代码一遍又一遍地尝试,堆栈无限地变得更加占用!(每次你调用一个方法,比如你的 getter,都会在堆栈中添加一个条目)

Every time the property is accessed and the getter is called, a new entry is added to the stack. Since this code will only tell other code to keep trying over and over, the stack infinitely become more occupied! (Every time you call a method, like your getter, an entry is added to the stack)

此外,您更新的问题询问为什么设置器会导致堆栈溢出.这与之前的原因几乎相同;Name 的setter 被调用时,它会调用自己. 简单的引用来解释?Name 回复您的代码时说:如果您想设置我的值,请尝试再次设置我的值?"

Also, your updated question asks why the setter would cause a stack overflow. It's pretty much the same reason as before; when Name's setter is called, it calls itself. Simple quote to explain? Name says in response to your code, "If you want to set my value, try setting my value again?"

它在哪里做的?在 Name = value; 中.您会尝试设置该值,但您的方法会告诉它再次设置自己.然后再次.再说一遍.

Where does it do that? In Name = value;. You would try setting the value, but your method would tell it to set itself again. And again. And again.

我们如何解决这个问题?正如其他回答者所示,您可以创建另一个变量来存储实际内容.例如,Name 将包含成功操作数据的 getter 和 setter.不过,它不会将其存储在自身中,以避免无限递归.它会将其存储在另一个变量中,例如 _Name.

How can we fix that? As the other answerer showed, you can create another variable that will store the actual content. For example, Name will contain the getters and setters to successfully manipulate data. It won't store it in itself though, to avoid infinite recursion. It'll store it in another variable, like _Name.

这篇关于属性将值设置为自身时如何发生堆栈溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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