Why is only a parameter-less Main method considered as a “valid startup object” for a C# project?
-
06-07-2019 - |
Question
I downloaded a zip of source files containing a C# project with multiple entry points / Main methods. Since I wanted to tinker around, I created another one like this in a new type/class
class MyExperiments
{
static void Main(String[] args)
{
// do something
}
}
then I switched to project properties. Simply switch the startup object to MyExperiments eh? To my surprise, the dropdown didn't have it. I rebuilt, made the method public, tried a whole lot of stuff.. but to no avail. Finally I edited the .csproj manually in notepad and then it worked. More tinkering around, I removed the parameters to make it
static void Main()
and now VS Project properties could 'see' the startup object. So now I could select it using the dropdown. I then added the String[] back and everything still worked.
Seems a bit weird to me (because the most common form is a Main method with parameters for command line args from the C/C++ times). MSDN says the dropdown will contain valid startup objects if they exist in your project.
Solution
Good thing you copy-pasted it, it is the capital 'S' in Main(String[] args)
. Apparently VS uses some text matching, and it's case sensitive. As it probably should be.
OTHER TIPS
lol - it looks like a bug in the IDE:
static void Main(String[] args) {}
doesn't show, but
static void Main(string[] args) {}
does!
Update: Response to the Connect Feedback / bug,
Thanks for the feedback! It looks the issue here is that the "String" parameter in the Main method needs to be a fully lowercase "string" (and it seems to have been pointed out on your stackoverflow post). I see a suggestion here to update the project property page to be slightly smarter about picking up the startup object, but given that there's a reasonable workaround, we're going to invest our resources in stabilizing and improving performance of VS2010. I'm going to go ahead and resolve the bug as a "Wont Fix" but please feel free to re-activate the bug if you have any further questions/comments.
Thanks, DJ Park C# IDE, Program Manager
So seems to be something you'd have to keep at the back of your mind for now - Gishu