The problem is that after I added the new class, the error came up when I did build the solution. What can be wrong?
In Form1, I don’t have any code yet.
I jus
If you have fixed the error and Visual Studio still gives you error message, it's worth to remove output folders (by default "bin" and "obj") and then rebuild the project. In my case just clicking on "rebuild" did not help.
A .NET program should have only one static Main
method.
You have two, and the compiler doesn't know which one to use.
Rename the pasted one, unless you want it to be the entry point to the application (in which case, rename the other), or compile the application passing using the /main
switch specifying which of the Main
methods to use.
See Main() and Command-Line Arguments (C# Programming Guide) on MSDN for more detail:
The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.
There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point. For more information, see /main (C# Compiler Options).
(emphasis mine)
A C# program can only have one Program.Main(). Main is the first method run when the program starts, so the compiler needs to know which one is the real one, and it can't if you have two.
It looks like you're making a Windows application. You should either add code to the existing main, or add it to an event handler triggered by your main form.
Others have pointed out that you have two static void Main methods. There are two easy fixes for this, one obvious and one that hasn't been specifically mentioned yet:
Main1
, NotMain
, etc.With solution 2, you can have identical Main(string[] args)
signatures in different classes without the compiler whining.
When you add a new class in your project and also you write the Main method, and when you run your code at that time it shows the error like "More than one Main method found", then you just need to:
An entry point may be chosen by add StartupObject
into your .cspoj
<StartupObject>MyApplication.Core.Program</StartupObject>
See -main (C# Compiler Options)