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

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.


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.


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.

Tags:

C#