Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由于输出中存在StackOverflowException,以下代码抛出进程正在终止。为什么?_C#_Stack Overflow - Fatal编程技术网

C# 由于输出中存在StackOverflowException,以下代码抛出进程正在终止。为什么?

C# 由于输出中存在StackOverflowException,以下代码抛出进程正在终止。为什么?,c#,stack-overflow,C#,Stack Overflow,我是C#新手,下面是我的代码,当我运行它时,它抛出进程终止,因为输出中存在StackOverflowException。为什么 namespace LearningCSharp { class Program { //I have created the below object, I have not use it in main() method // but still because this ouput says StackOverf

我是C#新手,下面是我的代码,当我运行它时,它抛出进程终止,因为输出中存在StackOverflowException。为什么

namespace LearningCSharp
{
    class Program
    {
        //I have created the below object, I have not use it in main() method 
        //  but still because this ouput says StackOverflowException
        Program ProgramObject = new Program();//removing "= new Program() " ,then it works fine

    static void Main(string[] args)
    {
        Program po = new Program();
        po.testing();
    }
    void testing()
    {
        Console.WriteLine("Testing is Ok");
    }
  }
}

主要问题是在自身内部创建
程序的实例:

namespace LearningCSharp
{
    class Program
    {
        Program ProgramObject = new Program(); // The runtime will try to create an instance of this when the class is created (instantiated)... but since the class is creating an instance of itself, that instance will also try to create an instance and so on... this goes on forever until there isn't enough memory on the stack to allocate any more objects - a stack overflow.

        ... other code here
    }
}
控制台应用程序需要在应用程序启动时调用的静态方法:

static void Main(string[] args)
此静态方法无法看到实例成员-因此您需要将
testing()
方法设置为静态:

    static void Main(string[] args)
    {
        testing();
    }

    static void testing()
    {
        Console.WriteLine("Testing is Ok");
    }
或者创建另一个可以实例化的类

namespace LearningCSharp
{
    class Program
    {

        static void Main(string[] args)
        {
            TestClass test = new TestClass();
            test.testing();
        }
    }
}

class TestClass 
{
    internal void testing()
    {
        Console.WriteLine("Testing is Ok");
    }
}

请注意,
testing()
方法的访问器应该是
internal
public
,否则
Main
类将无法访问它。

您可以这样创建它:

class Program
{

    static Program programObject = new Program();

    static void Main(string[] args)
    {
        Program po = new Program();
        po.testing();
        Console.ReadLine();
    }

    void testing()
    {
        Console.WriteLine("Testing is working fine!!!");
    }
}

致以最诚挚的问候。

程序生成的类有一个主入口方法,该方法必须是静态的,在程序启动时调用(它通常被标记为程序入口点,如果您愿意,可以更改)。如果需要测试该方法,您可以创建另一个类,在该类上放置可以实例化的方法,或者将
testing
方法标记为
static
,例如
static void testing()
,然后可以直接从
Main
方法调用它。实例化一个在实例化时自身实例化的类将溢出堆栈,因为您正在无休止地分配对象。
void Main
已输入<代码>=执行新程序()
。将创建
程序
的新实例。新实例有一个需要分配和初始化的类级变量
ProgramObject
。它是用
=new Program()
初始化的,因此创建了
程序的新实例。新实例有一个需要分配和初始化的类级变量
ProgramObject
。它是用
=new Program()
初始化的,因此创建了
程序的新实例。新实例有一个需要分配和初始化的类级变量
ProgramObject
。它已初始化…
主要问题是在其内部创建程序实例:
-该行本身不是问题所在。问题是该行与
程序po=new Program()的组合
void Main
中。一个没有另一个是无害的。因此,您甚至可以从
void Main
实例化
Program
,而无需将代码移动到单独的类中,只要您没有类级别变量。