I added a new class to my project and got an error saying “Program.Main() has more than one entry”. Why?

后端 未结 9 2287
萌比男神i
萌比男神i 2020-12-06 00:19

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

相关标签:
9条回答
  • 2020-12-06 00:58

    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.

    0 讨论(0)
  • 2020-12-06 01:01

    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)

    0 讨论(0)
  • 2020-12-06 01:02

    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.

    0 讨论(0)
  • 2020-12-06 01:02

    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:

    1. Rename one to anything else e.g. Main1, NotMain, etc.
    2. To set the /main compiler option mentioned by Habib, just right click on the project node in Solution Explorer, select Properties, and in the Application section select the "Startup object" in the dropdown.

    With solution 2, you can have identical Main(string[] args) signatures in different classes without the compiler whining.

    0 讨论(0)
  • 2020-12-06 01:06

    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:

    1. Right click on your project name which is shown in "Solution Explorer" then
    2. go into Application and in the Startup object dropdown menu choose your class which you want to run. If you don't find your class name in it, then simply just restart your IDE and again follow the above steps.
    0 讨论(0)
  • 2020-12-06 01:10

    An entry point may be chosen by add StartupObject into your .cspoj

    <StartupObject>MyApplication.Core.Program</StartupObject>
    

    See -main (C# Compiler Options)

    0 讨论(0)
提交回复
热议问题