Multiple Main Method in C#

In this article we will know how to handle a program with more than one entry point. All of us know the basic programming concept that is one program allow only and only one entry point i.e A Main Method. This is what normally expected. However, C# also allows you to create a program with one Entrance point its means one Main method.

but it will be very sound interesting to have more than one entry point in program. yeah! we can with illegal way and Infect with weird way. have a read below article to explore more.. 🙂

What is Method :-

Method or in other word we can also say that function is a collection of statement, rules embedded with its access specifier, return type and parameter having unique name resides in a class. A method or function can be Static or non static Function

What is Main Method

When a program starts, it looks for an entry point. This is the role of the Main() method. In fact, a program, that is an executable program, starts by, and stops with, the Main() method. The way this works is that, at the beginning, the compiler looks for a method called Main. If it doesn’t find it, it produces an error.

Main Method is the entry point for program execution. Hence we can say that without  Main Method we cannot imagine for a successful execution of a program.

Code Use  for exploring this article

What happen when we Execute This Code 

  • By using Ctrl+F5 and F5of Visual Studio IDE.
  • By using Visual Studio CMD Prompt(visual Stdudio Command Prompt) .

When we execute this code by using any method we will get error and the execution will be failed.

Lets us look what is error and what is solution to resolve the problem and execute the given code.

When using Visual Studio Editor

When we use this program in Visual Studio Editor and try to execute this program we will get error like given below

Error :- There were build errors. Would you  like to continue and run the last successful build?

According to your action :-

When you will click on Yes Button, A Warning will appear like

And if you will press No button then error( What is error in the Program) Will be Shown like given below:- 
What is Error in This Code

now  after taking three snaps you knew about the error. The error is that in this program Two Entry point is resides which is not legal programmatically in fact there should be only one entry point.

During compiling this program compiler will be in confusion which is Entry Point because Compiler get two Entry Point. Hence we can assume that one address two path. Which path have to follow.

How This Problem Will Be Solved

This problem can be resolved by specifying which Main is to be used to the compiler at the time of compilation.

When you Using Visual Studio Editor

then follow the steps given in image

  • Go to solution explorer, if solution explorer is not appear then press Ctrl+Alt+L .
  • right click on projectname, eg:- MultipleMainClasses and then
  •  click on properties.

After click on Properties A new Window Appear like given below

Initially no one Method is selected in start up object  you need to select.

  • Click on Application Tab.
  • Select Class with method which you want to execute as given in fig.
Multiple Main Method - ImgAfter select start up object execute Program by pressing f5.
This is the output of Class A Main Method
Multiple Main Method - Img1and This is the output of Class A Main Method
Multiple Main Method - Img2

Hey you have done it 🙂

When you Using Visual Studio CMD Prompt

then Try to execute as given below:

csc filename.cs /main:classname

where  filename is the name of the file where the code is stored and classname is the name of the class containing the Main which we would like to be the entry point.

As in given program there are two class A and B in which each containing A Main Method. We may write as

csc filename.cs /main:A           [ for Class    A  Main Execution ]   or,      

csc filename.cs /main:B            [  for Class    B  Main Execution ]

Conclusion

we can create a program having more than on entry point and also learned that how to handle it. but remember keeping more than one entry point is not legal as a rule so keep avoiding keeping more than one entry point.

41 thoughts on “Multiple Main Method in C#”

  1. Pincy Singh says:

    Thanks for making me understand we can have multiple entry point and use it as per our requirement.

  2. Virender says:

    very good artical it helps me a lot …thanks for sharing bro….

    1. Ravi Ranjan Kumar says:

      Thanks for your feedback.

  3. Neeraj Vasantkumar says:

    The analysis provided the partially correct.
    while compiling the above code using command prompt you need to specify fully qualified path of the class name.hence the command will look like :
    csc filename.cs /main:Multiple_MainClasses.B

    1. Ravi Ranjan Kumar says:

      Thanks for your valuable feedback.

  4. Ricky Sharma says:

    encountered an error In case of command prompt execution:
    could not find ‘A’ specified for main method

    1. Ricky Sharma says:

      its working in case of IDE

      1. Ravi Ranjan Kumar says:

        You can also use command prompt to execute same task, only difference is you’ve to externally define which main function you would like to invoke using cmd prompt.

    2. Ravi Ranjan Kumar says:

      There might be something wrong from your side while write code or may be in executing code.

  5. Kotesh says:

    Am getting following error :/

    A namespace cannot directly contain members such as fields or methods

  6. rajkumar says:

    very descriptive explanation. thank you

  7. balaram says:

    Ya Its nice but if that class is not displayed in the start up object menu what should we do
    please answer me….. i am getting same error

    1. Ravi Ranjan Kumar says:

      You can use command prompt to execute your program.

  8. Raman Kumar says:

    abey yarr dono method ek sath execute kr ke dikha

    1. Ravi Ranjan Kumar says:

      Raman bhai ek saath kabhi nahi hoga. Entry point to koi ek hi na hoga. agar dono gate khul gaya to bechara compiler confuse ho jaega ki kidhar se entry maru aur phir wo warning dega isse accha hai ki socho hi aisa karo hi mat.

  9. Vijay says:

    Hi I m really impressed thanks a lot for sharing…..pls keep it for ever

    1. Ravi Ranjan Kumar says:

      Thanks for your feedback Vijay.

  10. Sunil says:

    How to run from cmd prompt ?

    1. Ravi Ranjan Kumar says:

      As mentioned in article you can use given syntax
      csc filename.cs /main:classname

      if you have any problem then please revert back.

  11. Abhi says:

    Simply awesome. very useful for beginners. keep it up….

  12. Chandra Mani Gupta says:

    I was really surprised when i heard about multiple main methods declarations within a single program….but Ur so well elaborated examples and explanation made me satisfied…!! Thanks for sharing such wonderful stuffs..!!

    1. Ravi Ranjan Kumar says:

      Thanks for your feedback! I glad you liked it. Feel free to come back for more posts.

  13. naina says:

    but its not working …

    1. Ravi Ranjan Kumar says:

      hey whats not working.. please ask specific question, I will sure help you.

  14. Rohit Kumar (@krrohit0707) says:

    simple and good written.

    1. RaviRanjanKr says:

      Thanks for your feedback 🙂

  15. gaurav says:

    thanks

    1. RaviRanjanKr says:

      Most Welcome!

  16. Aamir says:

    Like it

    1. RaviRanjanKr says:

      My pleasure.

  17. Pingback: Experiencia con Java pero novato con C#
  18. Trackback: Experiencia con Java pero novato con C#
  19. adeebhat2012@gmail.com says:

    Thanks

  20. Lalit says:

    Nice post, helpful indeed. 🙂

  21. mrmbo7 says:

    thanks for this way to execute multi Main Method , but why we use this way ??

  22. aditya naik says:

    thanks yaar it really helped me
    i was not knowing about how to set the Multiple main methods properties.
    tanks 1nce again.

    1. RaviRanjanKr says:

      I glad to know it helped you.. thanks for visiting 🙂

  23. Minde Sachin Devram says:

    Very Nice representation and explanation. Thank you so much.
    Best of luck.

    1. RaviRanjanKr says:

      Thanks Sachin 🙂

  24. Siddharth Sharma says:

    Hope you are doing great..!!
    I am very much impressed by your writing style, Knowledge and everything.
    Keep up the good work.

    And Thank you for it friend. 🙂

  25. Siddharth Sharma says:

    That’s a great help friend.
    but is there any way through which we can execute both the main methods..
    one after the another or any other scheduling.

    1. RaviRanjanKr says:

      As I know You can only have multiple Main() method in your program but only one can be used as an entry point in that case others are meaningless. it recommended that we must inform the compiler which class need to execute as entry point while execution.

Leave a Reply