Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi friends

does main() method has public access modifier always?
Posted

Entry point (usually Main) should not be public. Actually, it should better be private and it is always created by Visual Studio as private. The access modifier is not written, it is public by default.

This fact is easy to check up by adding access modifier "private" explicitly — it does work.

Now, I want to explain why explanation my Manas is wrong: Main can perfectly be accessed by the Framework. In fact, access specifiers do not prevent Reflection to access types and members. This is easy to check up using BindingFlags.

—SA
 
Share this answer
 
Comments
[no name] 2-Aug-11 15:18pm    
Well Said.
Sergey Alexandrovich Kryukov 2-Aug-11 15:38pm    
Thank you, Collin.
--SA
Let's do a small experiment,

Step 1: Write a small program with following contents,

C#
namespace MainMethodTester
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main Method Tester");
        }
    }
}


Step 2: Build the solution.

Step 3: Open .Net Reflector program (I assume you have the program or otherwise, .Net Reflector[^]) and drag and drop the builded exe file(from the debug folder) in side the .Net Reflector program.

Step 4: Then Open the Main Method from the .Net Reflector window and you will see following code for the Main method,

C#
private static void Main(string[] args)
{
    Console.WriteLine("Main Method Tester");
}


in where Main is private BY DEFAULT.

Hope it helps :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-11 15:41pm    
Good points; this is what I said in first place. My 5.
--SA
Mohammad A Rahman 2-Aug-11 17:20pm    
Thanks SA. :)
Yes. Main has to have public access modifier.Reason being that Main is considered as the entry point of the application.It can be called within an assembly or even from different assembly.So how can another assembly can call a Main method that is not public.

More on MSDN :

http://msdn.microsoft.com/en-us/library/acy3edy3(v=vs.80).aspx[^]
 
Share this answer
 
Comments
walterhevedeich 30-Jul-11 12:49pm    
Deserves a 5. I was going to post the same link but was confused on the tags on OP's question. I would like to add that ASP.Net has a different notion with applications that has Main method as an entry point.
Manas Bhardwaj 30-Jul-11 12:51pm    
Thanks! Agree about your ASP.Net comment.
AZADKUMARREDDY 31-Jul-11 9:00am    
thanks for ur answer
Sergey Alexandrovich Kryukov 2-Aug-11 2:12am    
What happened? Unfortunately, you're wrong, as well as Manas. Please see my solution and comments. You can easily check up to make sure my answer is correct.
--SA
Sergey Alexandrovich Kryukov 2-Aug-11 2:11am    
Manas, what happened to you? Probably, it was not your day. It's easily to check up that your answer is... well, opposite to true. Please check up. Even your reference shows that Main is private. Look by yourself.

You explanation of why public access is needed is also wrong. I always access private members via Reflection. It is specifically documented that access modifiers do not provide any security.

Please see my answer where I explain things.

I did not vote. I suggest you remove this answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900