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


using System;
using System.Collections.Generic;
using System.Text;

namespace Multiple_MainClasses
{
    class A
    {
        static void Main(string[] args)
        {
            Console.WriteLine("I am from Class A");
            Console.ReadLine();
        }
    }
    class B
    {
        static void Main(string[] args)
        {

            Console.WriteLine("I am from Class B");
            Console.ReadLine();
        }
    }
}

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.
After select start up object execute Program by pressing f5.
This is the output of Class A Main Method
and This is the output of Class A Main Method

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.

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C# and tagged , . Bookmark the permalink.

38 Responses to Multiple Main Method in C#

  1. 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.

    • 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.

  2. 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. 🙂

  3. Minde Sachin Devram says:

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

  4. 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.

  5. mrmbo7 says:

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

  6. Lalit says:

    Nice post, helpful indeed. 🙂

  7. adeebhat2012@gmail.com says:

    Thanks

  8. Pingback: Experiencia con Java pero novato con C#

  9. Aamir says:

    Like it

  10. gaurav says:

    thanks

  11. naina says:

    but its not working …

  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..!!

  13. Abhi says:

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

  14. Sunil says:

    How to run from cmd prompt ?

  15. Vijay says:

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

  16. Raman Kumar says:

    abey yarr dono method ek sath execute kr ke dikha

    • 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.

  17. 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

  18. rajkumar says:

    very descriptive explanation. thank you

  19. Kotesh says:

    Am getting following error :/

    A namespace cannot directly contain members such as fields or methods

  20. Ricky Sharma says:

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

  21. 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

  22. Virender says:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s