private static void Main()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sujala

    private static void Main()

    In Java, the Main method cannot have any other access modifier than 'public'. How is it that in C# you can have a private access modifier to the Main method ? Can anybody throw any light, comparing JVM with CLR

    Thanks
    Sujala
  • Ignacio Machin \( .NET/ C#  MVP \)

    #2
    Re: private static void Main()

    Hi,

    If you refer to Main method as the entry point of the application, then you
    have a similar situation in C# than in Java, in c# the entry point MUST be
    declared as
    static void Main() , if that is not present the compiler will give you an
    error.

    Now there is nothing that prohibite you to name a method Main() on any
    class, only that it will no be the entry point of the application.


    Hope this help,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    "Sujala" <sujalaroy@hotm ail.com> wrote in message
    news:6C4941DD-0396-49DC-BDE6-9DD3588F6794@mi crosoft.com...[color=blue]
    > In Java, the Main method cannot have any other access modifier than[/color]
    'public'. How is it that in C# you can have a private access modifier to the
    Main method ? Can anybody throw any light, comparing JVM with CLR ?[color=blue]
    >
    > Thanks,
    > Sujala[/color]


    Comment

    • Sujala

      #3
      Re: private static void Main()

      Ignacio,

      Thanks for your answer. But its not true that "in c# the entry point
      MUST be declared as static void Main() , if that is not present the
      compiler will give you an error." Infact it works even if you declare
      the entry point as "private static void Main()" , the compiler gives no
      error, neither the CLR throws any run-time error !!! Isnt this strange
      ??!! Thats precisely the point of confusion. You may try this out.


      Thanks,
      Sujala

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Roy Fine

        #4
        Re: private static void Main()

        Sujala,

        public, private, protected and others are access attributes that are
        enforced primarily by the compiler (at runtime only when dynamic invocation
        is involved). Beyond that, the runtime is free to accept or ignore the
        access specifiers - and in the case of Main, it kindly ignores them.

        Matters such as these (and this is but one example of many others) are
        implementation specific details for the runtime, not for a particular
        language.

        regards
        roy fine


        "Sujala" <sujalaroy@hotm ail.com> wrote in message
        news:6C4941DD-0396-49DC-BDE6-9DD3588F6794@mi crosoft.com...[color=blue]
        > In Java, the Main method cannot have any other access modifier than[/color]
        'public'. How is it that in C# you can have a private access modifier to the
        Main method ? Can anybody throw any light, comparing JVM with CLR ?[color=blue]
        >
        > Thanks,
        > Sujala[/color]


        Comment

        Working...