Nour Ahmed Nour Ahmed - 2 years ago 75 Question

default constructor for main program vs default

Hello everyone i'm new to c# language i was use code:

Module Module1 ' main static class
Sub Main()
' output: constructor will be excuted
End Sub
Sub New()
Console.WriteLine("constructor will be excuted")
End Sub
End Module

but when i use c# the default constructor of class Program is Never executed

class Program

static void Main(string[] args)
// no output
public Program()
Console.WriteLine("c# the default constructor of class Program is Never executed");

Rob Rob
Answer Source

It's not executed because Main is executed as a static method, no instance of Program is created.

You can make your constructor static if you'd like, however it's likely you don't need the constructor at all, and you can move all your typical constructor code into Main

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download