简体   繁体   English

如何修复System.StackOverflow异常?

[英]How can fix System.StackOverflow Exception?

I used 2 classes, and I need send/receive methods and variables. 我使用了2个类,并且需要发送/接收方法和变量。 But when I make an instance of this class it gives me a System.StackOverflowException . 但是当我创建此类的实例时,它给了我System.StackOverflowException

How can fix this problem? 如何解决这个问题?

This is my code: 这是我的代码:

class Setup1
{
   Setup2 set2 = new Setup();

   int a = 5;

   public int myMethod();
   {
      set2.b =  a + 10;
      return set2.b;
   }
}

class Setup2
{
    Setup1 set1 = new Setup();
    public int b = 0;
    void  Show()
    {
        MessageBox.Show(set1.myMethod());
    }

}

You have an infinite recursion. 您具有无限递归。

In the constructor of Setup2() you call the constructor of Setup1() . Setup2()的构造函数中,调用Setup1()的构造函数。 There you call the constructor of Setup2() and so on, infinitely. 在那里,您无限调用Setup2()的构造函数,依此类推。 Your memory runs out, and your stack overflows. 您的内存用完,并且堆栈溢出。

似乎您有循环构造函数调用,导致堆栈溢出异常。

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

相关问题 C#system.stackoverflow异常 - C# system.stackoverflow exception 通过目录结构解析时发生System.StackOverflow异常 - System.StackOverflow Exception while Parsing through Directory Structure System.StackOverflow异常来自类之间的递归依赖 - System.StackOverflow exception from recursive dependency between classes C#/ WPF错误:发生类型为“ System.StackOverflow异常”的未处理异常(井字游戏) - C#/WPF Error: An Unhandled Exception of Type 'System.StackOverflow Exception' Occured (Tic-Tac-Toe) 执行不带搜索词的查询会导致System.Runtime.Serialization.dll中的System.StackOverflow异常 - Executing a query with no search term causes System.StackOverflow Exception in System.Runtime.Serialization.dll 我的Asp.net核心应用程序显示System.stackoverflow异常 - My Asp.net core application show System.stackoverflow exception 不知道如何修复此StackOverflow异常是未处理的错误 - Not sure how to fix this StackOverflow Exception was Unhandled error 如何修复Get / Set中的StackOverflow异常? - How to fix a StackOverflow exception in Get/Set? 如何避免此stackoverflow异常? - How to avoid this stackoverflow exception? 我们如何处理stackoverflow异常C# - How can we handle the stackoverflow exception C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM