-1

I am new to C#, following is my code, when I run it, it throws Process is terminating due to StackOverflowException in the output.Why??

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");
    }
  }
}
Khalid
  • 1
  • 2
  • The program generated class has a main entry method which is required to be static, this is called when the program starts (it's usually marked as the program entry point, you can change this if you like). If you need to test the method you either want to create another class on which you put the method that you can instantiate, or mark the `testing` method with `static` e.g. `static void testing()` then you can call it directly from the `Main` method. Instantiating a class which instantiates itself on instantiation is going to overflow the stack because you are endlessly allocating objects. – Charleh Feb 23 '20 at 09:55
  • `void Main` is entered. `= new Program()` is executed. A new instance of `Program` is created. The new instance has a class level variable `ProgramObject` that needs to be allocated and initialized. It is initialized with `= new Program()`, so a new instance of `Program` is created. The new instance has a class level variable `ProgramObject` that needs to be allocated and initialized. It is initialized with `= new Program()`, so a new instance of `Program` is created. The new instance has a class level variable `ProgramObject` that needs to be allocated and initialized. It is initialized wi... – GSerg Feb 23 '20 at 10:05

2 Answers2

0

The main issue is creating an instance of Program inside itself:

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
    }
}

A console application requires a static method that is called when the app starts:

static void Main(string[] args)

This static method cannot see instance members - so you will either need to make your testing() method static:

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

    static void testing()
    {
        Console.WriteLine("Testing is Ok");
    }

or create another class that you can instantiate

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");
    }
}

Note that the accessor for the testing() method should be internal or public or the Main class will not have access to it.

Charleh
  • 13,749
  • 3
  • 37
  • 57
  • `The main issue is creating an instance of Program inside itself:` - that line alone is not the issue. The issue is the combination of that line and the `Program po = new Program();` in `void Main`. One without the other is harmless. So you can even instantiate `Program` from `void Main` without moving the code to a separate class, as long as you don't have the class level variable. – GSerg Feb 23 '20 at 10:08
-1

You can create it like this:

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!!!");
    }
}

Best regards.

ali
  • 175
  • 1
  • 4
  • 21