Is staic void main() is necessary for entry point function in C# or we can use some other functions? Why main() is static?
Static Void Main() is the necessary entry point for any "Executable" (.EXE) to be created in C#. A library (or .DLL) can have other entry points.
The method is Static because that is required in order to access the method without having an instance of the object to address. In order to invoke the method (starting point) from outside the application, a static method is required.
static int Main(string[] args)
is static because it reduces the number of potential steps the runtime needs to go through before it can start executing the entry point of your program (run class's static constructor, run class's static Main... versus run class's static constructor, instantiate the class into some location that's undefined, run instance's constructor, then run instance's Main method). You'd have to write boilerplate code to store this
into an instance variable you could use (and likely to keep it from going out of referential scope which would enable it to be garbage-collected) which would also tend to increase the reference count and require a little more memory and pricessing for not much gain.
It's named Main because C# is strongly reminiscent of Java in its design, and Java uses the name Main, which itself derives from a slight differentiation (case is a thing!) from C and C++.
The required entry point is actually:
static void Main(string[] args); // note capitalization and arguments
The reason that Main
must be static is that non-static objects must be constructed before you call any methods on them. Since Main
is the entrance point of the program, who's going to call its constructor?
(Yes, you could have the CLR require that the class with Main
contain a default parameterless constructor, and make the CLR call that constructor during global startup. But that's extra work, and in practice it's easier to just require that Main
be static.)
Yes for a c# application, Main()
must be the entry point.
The reason is because that's what the designers of the language decided to be what to look for as an entry point for your program. They could just as well have used a totally different approach to find the entry point, e.g. using meta data, or instantiating an object for you (Which would require a parameterless constructor). Another reason for naming it void main()
is that it's intuitive for users coming from other languages.
Yes, it has to be Main
. It's static because otherwise the CLR would need to worry about creating an instance of the type - which means you'd presumably have to have a parameterless constructor, even if you didn't want an instance of the type, etc. Why would you want to force it to be an instance method?
The Main
method might be what you consider as the entry point of an application but in c# as far as i know methods can't be defined directly in namespaces which means it has to be within a class. The real first executed thing then is the static constructor of the class containing the Main
method
using System;
namespace test
{
class Program
{
static Program()
{
Console.WriteLine("static constructor");
}
public static void Main(string[] args)
{
Console.WriteLine("Main method");
}
}
}
Outputs static constructor
first and then Main method