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

后端 未结 9 2288
萌比男神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 01:11

    I experienced this issue after adding an xUnit test class to my .NET Core 2.1 project.

    The following article gives a detailed explanation of why, and provided the answer that worked for me - here.

    Basically, the compiler automatically generates a Main for the new class. You can provide a directive in your .csproj file to keep this from happening:

    <GenerateProgramFile>false</GenerateProgramFile>

    Add this to your <PropertyGroup> section and recompile.

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

    Having two Main methods is just fine. If you receive the error you mentioned then you need only to tell Visual Studio which one you'd like to use.

    1. Right-click on your project to view the properties.
    2. Go to the Application tab and choose the entry point you desire from the Startup object dropdown.

    Here's an example where I have two entry points depending on how I want to dev-test the assembly.

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

    You have two Main methods, and that is why you are getting this error.

    From MSDN - Main Method

    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.

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