Watch YouTube video here.
Abstract
The C# language standard requires that the Main() method always be the entry point of the program and triggers the program execution, but the class it is in is not specified by the standard. This article will elaborate a little more on this fact and will explain the possibility and issues of having multiple Main() methods, if at all possible, technically.
Normal Program structure
The code below shows a C# Console program code with only one Main() method.
Execute the application.
The program executes and successfully shows the message in the console.
Now let's add a second Main() into the program.
To do so there are two ways:
- Add a Program2 class to the same Program.cs file
- Add a separate C# class and define a Main() method in that.
Build and Observe the behavior
Now it's time to build the code and as soon as you build (Ctrl+Shift+B) two errors will be shown in the Error List window.

The Error description is as follows
Notice the Yellow highlighted text.
Error 1 Program 'c:\Users\vva597\Documents\Visual Studio 2013\Projects\ConsoleApplication\ConsoleApplication\obj\Debug\ConsoleApplication.exe' has more than one entry point defined: 'ConsoleApplication.Program.Main(string[])'. Compile with /main to specify the type that contains the entry point. c:\users\vva597\documents\visual studio 2013\Projects\ConsoleApplication\ConsoleApplication\Program.cs 7 21 ConsoleApplication
The Issue
Since we have added another Main() program the compiler is now confused about which one to use and it will now rely on the developer's decision to assign one specific Main() to the project for executon.
Go to Project -> Properties -> Aplication Tab -> Startup object.
You will see that both of the Main() method holders (classes) are listed there as shown in the image below.

Set a Startup object and Re-run
As shown in the previous image, select one of the Main() methods and execute the program. You shall see the two separate messages coming from the two different Main() methods as expected.


Amir NavedPosted Jul 18, 2023, 11:11 AM
Really Helpful saved my time
Akash KumharPosted Oct 22, 2016, 12:33 PM
Nice share
Sr KarthigaPosted May 6, 2016, 9:17 PM
good one sir
Vithal WadjePosted Mar 23, 2015, 1:39 PM
another outstanding article
Jeetendra GundPosted Mar 23, 2015, 1:22 AM
Great..
Sudheer JayachandranPosted Mar 21, 2015, 2:49 PM
Typo : both classes Program, Program2 have same strings to print
Khargesh RajputPosted Mar 21, 2015, 6:15 AM
nice sir
Sourabh SomaniPosted Mar 20, 2015, 9:28 PM
This is very common Interview question that can we have two main methods in c#? good one sir...
Tom MohanPosted Mar 20, 2015, 9:25 AM
useful
Gowtham RajamanickamPosted Mar 20, 2015, 4:52 AM
informative..