
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.

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.
Thanks for making me understand we can have multiple entry point and use it as per our requirement.
very good artical it helps me a lot …thanks for sharing bro….
Thanks for your feedback.
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
Thanks for your valuable feedback.
encountered an error In case of command prompt execution:
could not find ‘A’ specified for main method
its working in case of IDE
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.
There might be something wrong from your side while write code or may be in executing code.
Am getting following error :/
A namespace cannot directly contain members such as fields or methods
very descriptive explanation. thank you
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
You can use command prompt to execute your program.
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.
Hi I m really impressed thanks a lot for sharing…..pls keep it for ever
Thanks for your feedback Vijay.
How to run from cmd prompt ?
As mentioned in article you can use given syntax
csc filename.cs /main:classname
if you have any problem then please revert back.
Simply awesome. very useful for beginners. keep it up….
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..!!
Thanks for your feedback! I glad you liked it. Feel free to come back for more posts.
but its not working …
hey whats not working.. please ask specific question, I will sure help you.
simple and good written.
Thanks for your feedback 🙂
thanks
Most Welcome!
Like it
My pleasure.
Thanks
Nice post, helpful indeed. 🙂
thanks for this way to execute multi Main Method , but why we use this way ??
thanks yaar it really helped me
i was not knowing about how to set the Multiple main methods properties.
tanks 1nce again.
I glad to know it helped you.. thanks for visiting 🙂
Very Nice representation and explanation. Thank you so much.
Best of luck.
Thanks Sachin 🙂
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. 🙂
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.
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.