- Add a Program2 class to the same Program.cs file
- Add a separate C# class and define a Main() method in that.
Here for demo purposes I will use the #1 approach and a “class Program2” will be added to the same physical Program.cs file.
Build and Observe the behaviorNow 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 IssueSince 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-runAs 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.