locked
Main() of C# should not be public? RRS feed

  • Question

  • On Main() and Command Line Arguments (C# Programming Guide) , there is a statement:

    It (the Main method) is declared inside a class or struct. It must be static and it should not be public. (In the example above it receives the default access of private.)

    In contrast, the main method of Java MUST be public. Could anybody explain why the Main method of C# should not be public?

    Thanks.

    Sunday, November 4, 2007 9:34 AM

Answers

  • Due to the way the .NET runtime invokes the Main method, it is able to bypass the access visibility of the Main method.  By not declaring it public, you improve encapsulation, the OOP principle that essentially you should restrict access to members as much as feasible.

     

    Note that nothing bad is likely to happen if you instead decide to declare Main public.

    Sunday, November 4, 2007 3:12 PM
  • Thanks for your reply.

    Actually, even though the C# Programming Guide says that the Main method should not be public, public static void Main is very common and can be found in the C# Language Specification.

    Sunday, November 4, 2007 4:08 PM
  • I wasn't aware of the change in the visibility recommendation of Main. But it makes sense to me somewhat, to restrict it. If you don't really intend on letting Main() be directly callable by an outsider, but only by the CLR itself, then you wouldn't want to make it publically visible, or an outsider could invoke it (unless they don't even have access to your class, such as if the class itself is not public).

     

    Monday, November 5, 2007 3:10 PM
  • It's been the same for C# 1.0, 1.1 and 2.0. The Main( ) entry point function can be declared as private or public. It is preferred to declare it as private to "enforce encapsulation". What does this mean? In other words, it prevents other .NET assemblies from directly calling your Main( ) function to start you application. The .NET framework must start it.

     

    Main( ) FUN FACT: You can define several Main( ) functions in different classes of your .NET project, so long as you specify which class holds the "golden" Main( ) function when you compile it. You typically do this for different types of builds. Just specify it in your project's properties or as a passed in command line option. 

     

    Main( ) FUN FACT: VB.NET does not allow you to define Main( ) as Private. It must be Public.

     

    Good luck!

    Tuesday, November 6, 2007 10:53 PM

All replies

  • Due to the way the .NET runtime invokes the Main method, it is able to bypass the access visibility of the Main method.  By not declaring it public, you improve encapsulation, the OOP principle that essentially you should restrict access to members as much as feasible.

     

    Note that nothing bad is likely to happen if you instead decide to declare Main public.

    Sunday, November 4, 2007 3:12 PM
  • Thanks for your reply.

    Actually, even though the C# Programming Guide says that the Main method should not be public, public static void Main is very common and can be found in the C# Language Specification.

    Sunday, November 4, 2007 4:08 PM
  • Ah... I have found the following statement on http://msdn2.microsoft.com/en-us/library/aa309381(VS.71).aspx:

     

    In Visual C#, all code must be contained in methods of a class. So, to house the entry-point code, you must first create a class. (The name of the class does not matter here). Next, you specify the entry point itself:

    public static void Main () {

    The compiler requires this to be called Main. The entry point must also be marked with both public and static. In addition, as with the Managed Extensions for C++ example, the entry point takes no arguments and does not return anything (although different signatures for more sophisticated programs are certainly possible).

     

    It seems that public is required in .NET Framework 1.x. Why has MUST been changed to SHOULD NOT since version 2?

    Sunday, November 4, 2007 4:43 PM
  • I wasn't aware of the change in the visibility recommendation of Main. But it makes sense to me somewhat, to restrict it. If you don't really intend on letting Main() be directly callable by an outsider, but only by the CLR itself, then you wouldn't want to make it publically visible, or an outsider could invoke it (unless they don't even have access to your class, such as if the class itself is not public).

     

    Monday, November 5, 2007 3:10 PM
  • It's been the same for C# 1.0, 1.1 and 2.0. The Main( ) entry point function can be declared as private or public. It is preferred to declare it as private to "enforce encapsulation". What does this mean? In other words, it prevents other .NET assemblies from directly calling your Main( ) function to start you application. The .NET framework must start it.

     

    Main( ) FUN FACT: You can define several Main( ) functions in different classes of your .NET project, so long as you specify which class holds the "golden" Main( ) function when you compile it. You typically do this for different types of builds. Just specify it in your project's properties or as a passed in command line option. 

     

    Main( ) FUN FACT: VB.NET does not allow you to define Main( ) as Private. It must be Public.

     

    Good luck!

    Tuesday, November 6, 2007 10:53 PM