A Class Instantiating an Instance of Itself??

Recondaddy

Newcomer
Joined
Sep 19, 2012
Messages
5
Hello everyone,

I'm not a software engineer, so I'm certain this is probably something that you guys see all the time, but it's a first for me.

I'm reverse engineering a sample C# console application, and I ran across a scenario I don't quite understand -- a class instantiating itself within itself. Here's a simplified example of what I mean:

Code:
class Program
    {
        static void Subroutine1()
        {
           DoSomething
        }

        static void Main(string[] args)
        {
            Subroutine1();

            [B]Program program = new Program();[/B]
            ...
            ...
            ...
            program.Subroutine2();
        }

        void Subroutine2()
        {
             DoSomethingElse
        }
     }

I hope that's enough to see what I'm talking about. Does anyone know what the developer was trying to accomplish here?

Thanks for any help you can provide.
 
In that example the class is being instantiated from the Main method, this method is declared as static and cannot access any instance members only static ones such as Subroutine1 , by creating an instance of the Program class the Main method can access instance members such as Subroutine2 through the program instance.
 
Back
Top