Share via


What is difference between public static void Main and static void Main ?

Question

Thursday, February 18, 2016 10:58 AM

main function is by default public if we do not use public keyword ?

see the code

class Program
    {
        static void Main(string[] args)
        {

        }
    }

why main function is public static ?

please discuss this

All replies (9)

Thursday, February 18, 2016 11:14 AM ✅Answered | 1 vote

public -> you can call main from everywhere , private -> you can not call this main from other assemblies

static -> your program is an independent object. it doesn't depend on any other

void -> this main does not return a value

main -> main special word defines the entry point to your program

string[] args -> arguments that can be pass in the command window

Hope it helps. Spiri


Thursday, February 18, 2016 11:15 AM ✅Answered

It is static so that an instance of the Program class isn't required in order to call it (since it is the entry point of the program the operating system, in effect, has to be able to just call it directly).

As to it's access modifier, this msdn link states that it must not be public, which I think is more of a sensible guideline to prevent some other class mistakenly calling it rather than a compiler-enforced rule.

That link also says that by not specifying an access modifier it becomes private by default. E.g. the equivalent of writing:

 private static void Main(string[] args)

Thursday, February 18, 2016 3:03 PM ✅Answered

>>main function is by default public if we do not use public keyword ?

No. The default access modifier of the Main method is private. The Main must be static but it should not be public. Please refer to the following page for more information: https://msdn.microsoft.com/sv-se/library/acy3edy3.aspx

>>why main function is public static ?

It shouldn't be public since it is only supposed to be called by the CLR once when the application starts.

It is static for the CLR to be able to call it directly without creating an instance of the class first. This is the way the designers of the C# programming language decided to look for an entry point of an application. That's why it doesn't have to be public.

Hope that helps.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


Thursday, February 18, 2016 10:21 PM ✅Answered | 1 vote

Normally a private method cannot be called from outside the class that it is in. But the Main method is a special case. There can be only one method in a program with that name, and the operating system (or the CLR) knows to call that method to start the program.

You really do not need to know how this happens. Just know that the Main method is where your program starts, and that it is recommended to be private but will still work if it is public.


Friday, February 19, 2016 4:33 AM ✅Answered | 1 vote

 There can be only one method in a program with that name, and the operating system (or the CLR) knows to call that method to start the program.

Hi ,

We can overload the main method but entry points is considered only main method which have signatures either Main() or Main(string[] args).Also note that both these signatures can not be overloads and can not exists in same program as two entry points(because only one entry point is allowed when clr calls internally). One can be either of two and another can be any signature different then two of these.

 Clr automatically finds the method matching the predefined signature and treats it as an entry point.. Most probably it uses reflection.

https://msdn.microsoft.com/en-us/library/ms228506(v=vs.90).aspx

https://social.msdn.microsoft.com/Forums/vstudio/en-US/9184c55b-4629-4fbf-ad77-2e96eadc4d62/why-is-main-in-c-not-a-public-static-?forum=csharpgeneral

using System;
namespace ConsoleApplication1
{
    class Program
    {

        //Main can have only signature Main(string[] args) or Main(). Only one of these signature can exists in method.
        // Other than these two signature any other will not be treated as entry point

        
        //Only this works and remove static void Main()
        static void Main(string[] args)
        {
            Console.WriteLine("hello from Main(string[] args)");
            Console.ReadKey();
        }


        //Only This works and remove Main(string[] args)
        static void Main()
        {
            Console.WriteLine("hello from Main()");
            Console.ReadKey();
        }

        //If both of above main it will show error 
        //has more than one entry point defined


        //method with warning
        //Warning   3   'ConsoleApplication1.Program.Main(int)' has the wrong signature to be an entry point    
       static void Main(int i)
        {
            Console.WriteLine("hello from int");
            Console.ReadKey();
        }
    }
}

A user friendly computer first requires a friendly user


Thursday, February 18, 2016 4:12 PM

main() method is just an entry point for your application and can be private( default).

Making it public means allowing it to be called from outside or it can be helpful in case of automated testing.

A user friendly computer first requires a friendly user


Thursday, February 18, 2016 7:08 PM

1) u said : since it is the entry point of the program the operating system. i guess CLR call the main function. if i am wrong then rectify me with right information. how operating system comes into scene for calling the main function?

2) u said : As to it's access modifier, this msdn link states that it must not be public, which I think is more of a sensible guideline to prevent some other class mistakenly calling it rather than a compiler-enforced rule.

please elaborate this point u said "to prevent some other class mistakenly calling it rather than a compiler-enforced rule"

3) when we do not specify any access specifier like public that means it is private. how a private main function can be call from out side of program ? explain it in detail.


Thursday, February 18, 2016 7:09 PM

How CLR can call a private main function from out side ?

guide me in detail. thanks


Friday, February 19, 2016 6:27 PM

these two main function in program class is possible because below two functions are overloaded.

//Only this works and remove static void Main()
        static void Main(string[] args)
        {
            Console.WriteLine("hello from Main(string[] args)");
            Console.ReadKey();
        }


        //Only This works and remove Main(string[] args)
        static void Main()
        {
            Console.WriteLine("hello from Main()");
            Console.ReadKey();
        }