我在属性 C# 的 set 函数上的嵌套类中收到 StackOverflowException 错误

发布于 2024-10-04 09:53:43 字数 634 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦里梦着梦中梦 2024-10-11 09:53:43

你的财产正在自行设置。

属性设置器中的行 prop2 = ... 调用属性设置器,属性设置器再次调用自身,再次调用自身,再次调用自身,再次调用自身,再次调用自身,再次调用自身再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己,再次调用自己再次调用自身,再次调用自身,再次调用自身,再次调用自身...

getter 执行相同的操作,只是您从不调用它。

您需要为要获取和设置的属性创建一个支持字段。

例如:

private string prop2; //Create a backing field
public string Prop2 {
    get { return prop2; }
    set { prop2 = EditString(value); }
}

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:

private string prop2; //Create a backing field
public string Prop2 {
    get { return prop2; }
    set { prop2 = EditString(value); }
}
廻憶裏菂餘溫 2024-10-11 09:53:43

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.

清风夜微凉 2024-10-11 09:53:43

您在 get 中返回 prop2,这会导致堆栈溢出(无限递归是不好的)。

You are returning prop2 in the get, that causes the stack overflow (infinite recursion is bad).

夜无邪 2024-10-11 09:53:43

您处于无限循环中,因为您正在将属性 (prop2) 设置为其自身。

You are in an infinite loop because you are setting the property (prop2) to itself.

〃安静 2024-10-11 09:53:43

错误出现在 prop2 的定义中。 get 和 set 方法都只是回调到 prop2 属性,从而引发无限递归。

public string prop2 { 
  get { return prop2; // <-- This just calls prop2 get again}
}

您需要在此处添加一个支持字段来存储属性的值,如下所示

private string m_prop2;
public string prop2 { 
  get { return m_prop2; } 
  set { m_prop2 = EditString(value); } } 

The error is in the definition of prop2. Both the get and set methods simply call back into the prop2 property and hence induce infinite recursion.

public string prop2 { 
  get { return prop2; // <-- This just calls prop2 get again}
}

You need to add a backing field here to store the value of the property like so

private string m_prop2;
public string prop2 { 
  get { return m_prop2; } 
  set { m_prop2 = EditString(value); } } 
自由如风 2024-10-11 09:53:43

抛出异常时您是否查看过调用堆栈?您应该看到对 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.

蓝色星空 2024-10-11 09:53:43

您正在将属性设置为自身

You are setting to the property to itself

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文