Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, November 10, 2009 4:49 PM
Can I have multiple Main() Methods in console/windows application?
If yes, how do I let the program know which one to use (I know we can use switch keyword in command prompt, but I am interested to know how it can be done from Visual Studio) ?
Also, how can I programmatically switch between the multiple Main() methods ?
Is it a good design to have multiple Main() methods ?
All replies (6)
Tuesday, November 10, 2009 6:22 PM âś…Answered
You can have multiple Main methods in a C# program, and specify which one to use with the /main switch. However, the class containing the Main routine can only have a single static Main function.
For details, see Main() on MSDN, as well as the /main compiler option.
Reed Copsey, Jr. - http://reedcopsey.com
Tuesday, November 10, 2009 4:52 PM
If I am not mistaken, you cannot have multiple methods with the same signature.
You can, however, setup multiple static methods that the Main() method uses.
It would really help if you could explain what you are trying to accomplish.public enum Answers { Everything = 0x2A }
Thursday, November 12, 2009 9:57 AM
Hi,
Yes, you can have multiple main methods in your console/windows application within multiple classes but a single class can't have more than one main method.
Steps to set startup main method:
- Go to solution properties.
- Select application tab.
- Set startup object from the dropdown list, if you don't do so your application will not compile.
-Amit
Thursday, January 13, 2011 3:20 PM
What if the projet IS a "console application" but was not as such, but as an "empty project" ? Indeed, The classes do not appear in the referred dropdown list.
Thursday, January 13, 2011 3:30 PM
What if the projet IS a "console application" but was not created as such, but as an "empty project" ? Indeed, The classes do not appear in the referred dropdown list.
Thursday, January 13, 2011 4:09 PM
The type of project is irrelevant. All classes containing a Main method with the right signature should appear in the list.
Check that your Main method takes no parameters, or a single array of strings. Check that your Main method returns nothing, or an int.