Nour Ahmed Nour Ahmed - 7 months ago 16
Vb.net Question

default constructor for main program vs vb.net default

Hello everyone i'm new to c# language i was use vb.net
vb.net 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

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