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
static void Main(string args)
Console.WriteLine("I am from Class A");
static void Main(string args)
Console.WriteLine("I am from Class B");
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
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 ]
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.