我在属性 C# 的 set 函数上的嵌套类中收到 StackOverflowException 错误
public class Class1
{
public Class1()
{
prop = new Class2();
}
public Class2 prop { get; set; }
public class Class2
{
public Class2()
{
this.prop2 = "nikola";
}
public string prop2 { get { return prop2; } set { prop2 = EditString(value); } }
public string EditString(string str)
{
str += " plavsic";
return str;
}
}
}
这是我的代码,我有问题。当我尝试初始化 Class1 类型的对象时,它会抛出 StackOverflowException 错误。我做错了什么?
public class Class1
{
public Class1()
{
prop = new Class2();
}
public Class2 prop { get; set; }
public class Class2
{
public Class2()
{
this.prop2 = "nikola";
}
public string prop2 { get { return prop2; } set { prop2 = EditString(value); } }
public string EditString(string str)
{
str += " plavsic";
return str;
}
}
}
this is my code that i have problem with. When i try to initialise object that is type of Class1 it throws an StackOverflowException error. what am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的财产正在自行设置。
属性设置器中的行
prop2 = ...
调用属性设置器,属性设置器再次调用自身,再次调用自身,再次调用自身,再次调用自身,再次调用自身,再次调用自身再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己再次调用自身,再次调用自身,再次调用自身,再次调用自身...getter 执行相同的操作,只是您从不调用它。
您需要为要获取和设置的属性创建一个支持字段。
例如:
Your property is setting itself.
The line
prop2 = ...
in the property setter calls the property setter, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again, which calls itself again ...The getter does the same thing, except that you never call it.
You need to make a backing field for the property to get and set.
For example:
Prop2 设置/返回 Prop2... 调用 Prop2 来获取/设置 Prop2 的值,Prop2 调用 Prop2... 看看这是哪里?
这种情况不断发生,直到计算机/运行时耗尽存储调用堆栈的空间,然后死亡。
Prop2 sets/returns Prop2... which calls Prop2 to get/set the value of Prop2, which calls Prop2... see where this is headed?
That keeps happening until the computer/runtime runs out of space to store the call stack, and dies.
您在 get 中返回 prop2,这会导致堆栈溢出(无限递归是不好的)。
You are returning prop2 in the get, that causes the stack overflow (infinite recursion is bad).
您处于无限循环中,因为您正在将属性 (prop2) 设置为其自身。
You are in an infinite loop because you are setting the property (prop2) to itself.
错误出现在
prop2
的定义中。 get 和 set 方法都只是回调到prop2
属性,从而引发无限递归。您需要在此处添加一个支持字段来存储属性的值,如下所示
The error is in the definition of
prop2
. Both the get and set methods simply call back into theprop2
property and hence induce infinite recursion.You need to add a backing field here to store the value of the property like so
抛出异常时您是否查看过调用堆栈?您应该看到对 prop2 的 setter 的无休止的调用列表。
Have you looked in the call stack when the exception is thrown? You should see an endless list of calls to the setter for prop2.
您正在将属性设置为自身
You are setting to the property to itself