How to Resolve Error "CS0017: Program has more than one entry point defined" in C#
The Compiler Error CS0017 is an ambiguity error. The message reads: "Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point."
In C#, an executable application (like a Console App or Windows Forms App) must have exactly one starting point, i.e. only one main method. This is traditionally the static void Main(string[] args) method. If the compiler scans your project and finds two or more classes that contain a valid Main method, it does not know which one to launch when the application starts.
This guide explains how to resolve this conflict by either removing the duplicate entry point or configuring the project to select a specific one.
Understanding the Entry Point
When a C# program starts, the CLR (Common Language Runtime) looks for a method with a specific signature to begin execution:
public static void Main()
// OR
public static void Main(string[] args)
// OR (Async versions)
public static Task Main()
A single project can contain hundreds of classes, but only one of them is allowed to be the "Captain" that starts the ship.
Top-Level Statements: In .NET 6+, if you have code sitting loosely in a file (without a class wrapper), the compiler generates a Main method for you automatically. If you also have a class with an explicit Main method in the same project, this will trigger CS0017.
Reproducing the Error (The Conflict)
This error often happens when developers copy code from another project (including its Program.cs) or create a temporary Main method in a second class to test a specific feature.
Example of Code with Two Entry Points
// File: Program.cs
namespace MyApp
{
class Program
{
// Entry Point A
static void Main(string[] args)
{
System.Console.WriteLine("Main Program running...");
}
}
}
// File: TestScript.cs
namespace MyApp
{
class TestScript
{
// ⛔️ Error: Entry Point B
// The compiler sees this as a second valid candidate for startup.
static void Main(string[] args)
{
System.Console.WriteLine("Test Script running...");
}
}
}
Build Output:
error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
Solution 1: Rename or Remove the Extra Main Method
If the second Main method was just for testing or copied by accident, the simplest fix is to rename it or delete it.
// File: TestScript.cs
namespace MyApp
{
class TestScript
{
// ✅ Correct: Renamed to 'RunTest' so it is no longer an entry point candidate.
static void RunTest()
{
System.Console.WriteLine("Test Script running...");
}
}
}
Solution 2: Configure the Startup Object
Sometimes you intentionally want multiple entry points in a single project (e.g., one for the standard app, one for a debug mode, or one for a migration tool), and you want to choose which one runs via configuration.
You can tell the compiler explicitly which class to use.
Option A: Visual Studio Properties
- Right-click your Project in Solution Explorer.
- Select Properties.
- Under the Application tab (General), look for the Startup object dropdown.
- Select the specific class you want to launch (e.g.,
MyApp.Program).
Option B: Edit .csproj File
You can define this directly in the XML project file. This corresponds to the /main compiler switch mentioned in the error message.
- Right-click the project > Edit Project File.
- Add the
<StartupObject>tag inside a<PropertyGroup>.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<!-- ✅ Correct: Explicitly tell the compiler to start with 'MyApp.Program' -->
<StartupObject>MyApp.Program</StartupObject>
</PropertyGroup>
</Project>
If you are using Top-level statements (code without a class), the hidden class generated by the compiler is typically named Program. If you want to use the Top-level statements as the entry point over another class, set <StartupObject>Program</StartupObject>.
Conclusion
CS0017 ensures that the execution path of your application is unambiguous.
- Check for Accidental Mains: Look for
static void Mainmethods in classes where they shouldn't exist (often left over from testing). - Rename them: Change
MaintoStartorRunif they are not intended to be the application entry point. - Set Startup Object: If you deliberately have multiple entry points, configure your
.csprojfile to specify which one holds the authority to start the app.