Multiple Main() Methods
- As you know already, Main() method is nothing but the entry point of the application.
- Most commonly, a .NET application contains only one Main() method.
- If needed, you are supposed to define multiple Main() methods also.
- But, at run time, only one Main() method can be specified as ―Entry Point‖. This specification can be changed using the project properties.
- To understand better, we start with an example on this
Download above Code of Demo on Multiple Main() Methods : Downloads Links
- When this program is compiled, the compiler shows 2 compile time errors.
- It‘s because, two Main() methods are found in MySample class and Program class; So that the compiler can‘t understand which Main() method is to be used as exact entry point.
- To specify the required entry point, we have to change ―Startup Object‖ option in the project properties.
- To open the project properties, click on Project menu Properties.
- Then the project properties will be opened. Now, observe the Startup Object option.
- The Startup Object option contains two options.
---> MultipleMainMethods.Program
(MultipleMainMethods‖ is the project name).
- Whenever it is set to “(Not Set)”, C# compiler automatically detects the Main() method, where it is exists. This is the default value in the ―Startup object‖ option. But this fails whenever multiple Main() methods are defined.
- Now you have to select the required class that contains the desired Main() method as entry point.
- Finally close the properties tab and come to ―Program.cs‖ tab.
- Now run the application.
- Then you can get the output from the desired Main() method
No comments:
Post a Comment