Main Method in C#

See following points,

  1. Main() method is the starting point of a program.

  2. Main() method is always "Static".

  3. Main() should NOT be "Public". Default access specifier of Main() is Private.

  4. Following are the acceptable signatures of Main() method,
    1. static void Main(string[] args){}  
    2.   
    3. static void Main(){}  
    4.   
    5. staticint Main(string[] args){}  
    6.   
    7. staticint Main(){}  
  5. Main() method can have either of the two return types - void and int.

  6. Main() method can either have input string arguments or none.

  7. There can be only one entry point in a program. However, we can have multiple Main() in the same project. But you need to define the "Startup object" defining which is the single entry point.

    Sample Code:

    We have two classes,
    1. namespace ConsoleApplication2  
    2. {  
    3.     //Class 1  
    4.     public class Class1  
    5.     {  
    6.         static void Main(string[] args)  
    7.         {}  
    8.     }  
    9.     //Class 2  
    10.     public class Class2  
    11.     {  
    12.         static void Main(string[] arg)  
    13.         {}  
    14.     }  
    15. }  
    When you compile this program, you can see both classes listed as Startup objects in Property pages of the Project.

    You need to select one of the two.

Ebook Download
View all
Learn
View all