遇到无限循环问题。进程由于StackOverflowException而终止? [英] Getting Infinite Loop Issue. Process Terminated due to StackOverflowException?

查看:107
本文介绍了遇到无限循环问题。进程由于StackOverflowException而终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace ConsoleApplication1
{

class class1
{
    protected internal string inf1()
    {
        Console.WriteLine("\n......inf1() \n");

        return inf1();
    }
}




class class2 :class1
{
    static void Main(string[] args)
    {
        class1 c1 = new class1();

        class2 c2 = new class2();

        Console.WriteLine(c1.inf1());

        Console.WriteLine(c2.inf1());

        Console.ReadKey();
    }
}

获取无限循环问题。进程由于 StackOverflowException 终止了?

Getting Infinite Loop Issue. Process Terminated due to StackOverflowException ?

如何防止代码无限循环?

How to prevent the code from looping infinitely ?

推荐答案

class2 中,您正在调用 Console.WriteLine(c1.inf1 ());

所以 class1.inf1 应该在尝试将其输出到控制台时返回一个字符串。

So class1.inf1 should return a string as you are trying to output it to the console.

但是, class1.inf1()递归地调用自身而没有退出并且不返回字符串。

However, class1.inf1() recursively calls itself with no exit and does not return a string.

所以我认为这可能是您要实现的目标:

So I think this may be what you are trying to accomplish:

protected internal string inf1()
{
    return "\n......inf1() \n";
}

这篇关于遇到无限循环问题。进程由于StackOverflowException而终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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