StackOverflowException 错误

StackOverflowException Error

我遇到一个问题,我将我的第一个数组放入一个名为 DisplayArray1() 的 public 方法,我编译了它,当我打开可执行文件时显示“Process由于 WhosebugException 而终止”?还有其他人遇到过这个问题吗?

到目前为止,这是我的代码:

using System;

namespace FlexibleArrayMethod
{
    class Program
    {
        static void Main()
        {
            Console.Clear();

            // Call intDisplayArray1() to output on screen
            intDisplayArray1();
            Console.Write("Array 1:  ");
        }
        public static int intDisplayArray1()
        {
            // first array declaration
            int[] Array1 = {5, 10, 15, 20};

            return intDisplayArray1[];
        }
    }
}

感谢任何帮助!

假设 return intDisplayArray1[];(无法编译)在您的代码中确实是 return intDisplayArray1();,您处于递归循环中。

您在没有退出条件的情况下重复调用您的方法。看起来你真的只想 return 你的数组:

 public static int[] intDisplayArray1()
 {
    int[] Array1 = { 5, 10, 15, 20 };

    return Array1;
 }

尽管仍然存在大量逻辑错误。

这是我被引导相信你正在尝试做的事情:

static void Main()
{
    Console.Clear();

    // Call intDisplayArray1() to output on screen
    int[] array1 = intDisplayArray1();
    Console.Write("Array 1: " + string.Join(",", array1));
    Console.Read();
}

public static int[] intDisplayArray1()
{
   // first array declaration
   int[] Array1 = { 5, 10, 15, 20 };

   return Array1;
}