user1544479
user1544479

Reputation: 1007

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

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 just added a new class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;

namespace OpenHardwareMonitorReport
{

    class Program
    {

        static void Main(string[] args)
        {
            Computer computer = new Computer();
            computer.Open();

            var temps = new List<decimal>();
            foreach (var hardware in computer.Hardware)
            {
                if (hardware.HardwareType != HardwareType.CPU)
                    continue;
                hardware.Update();
                foreach (var sensor in hardware.Sensors)
                {
                    if (sensor.SensorType != SensorType.Temperature)
                    {
                        if (sensor.Value != null)
                            temps.Add((decimal)sensor.Value);
                    }
                }
            }

            foreach (decimal temp in temps)
            {
                Console.WriteLine(temp);
            }
            Console.ReadLine();
        }
    }
}

Then I see file Program.cs and the error on Main():

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace NvidiaTemp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Error 2  Program 'D:\C-Sharp\NvidiaTemp\NvidiaTemp\NvidiaTemp\obj\x86\Debug\NvidiaTemp.exe' has more than one entry point defined: 'NvidiaTemp.Program.Main()'. Compile with /main to specify the type that contains the entry point. D:\C-Sharp\NvidiaTemp\NvidiaTemp\NvidiaTemp\Program.cs  14  21  NvidiaTemp

Upvotes: 64

Views: 120131

Answers (12)

aar
aar

Reputation: 1

Program 'output file name' has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

A program can only have one Main method.

To resolve this error, you can either delete all Main methods in your code, except one, or you can use the StartupObject compiler option to specify which Main method you want to use.

https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0017

Just go to "StartupObject compiler option" and select your program as startup object.

Upvotes: 0

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23602

My demo project had the Microsoft.NET.Test.Sdk Nuget package left over from some tinkering.

Removing this Nuget package resolved the error even though I had no tests or test projects in the solution.

Upvotes: 1

Jpsy
Jpsy

Reputation: 20852

Did you recently add unit testing (XUnit) to your project?
Then this is the reason for you error message. You can read about the details in this blog post.

The solution is simple:
Edit your .csproj file and add a GenerateProgramFile property with value false to a general PropertyGroup (one that has no conditions):

Example:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <IsPackable>false</IsPackable>
  <GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

Upvotes: 6

Maksym Anurin
Maksym Anurin

Reputation: 3947

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

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

See -main (C# Compiler Options)

Upvotes: 7

Parth
Parth

Reputation: 121

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.

Upvotes: 4

Bill Tarbell
Bill Tarbell

Reputation: 5234

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.

Enter image description here

Enter image description here Enter image description here

Upvotes: 19

Jesse Q
Jesse Q

Reputation: 1661

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.

Upvotes: 99

Paul Smith
Paul Smith

Reputation: 3216

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.

Upvotes: 38

Habib
Habib

Reputation: 223237

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.

Upvotes: 6

Sielu
Sielu

Reputation: 1110

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.

Upvotes: 3

Oded
Oded

Reputation: 498972

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)

Upvotes: 10

DGH
DGH

Reputation: 11539

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.

Upvotes: 24

Related Questions