Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code: "Program has more than one entry point defined"

I created a C# project using Visual Studio Code. This project contains two .cs files, Addition.cs and Substraction.cs. Both files contain a main() function and both files contain two different programs.

Code in the Addition.cs file:

using System;

namespace Example
{
    class Addition
    {
        static void Main(string[] args)
        {
            int sum = 3 + 2;
            Console.WriteLine(sum);
        }
    }
}

Code in the Substraction.cs file

using System;

namespace Example
{
    class Substraction
    {
        static void Main(string[] args)
        {
            int sub = 3 - 2;
            Console.WriteLine(sub);
        }
    }
}

I want to test both the programs one by one, but when I do

"dotnet run"

It fails with the above error.

I know because of two main() functions (entry points) in the same project is creating this error, but this can be overcome in Visual Studio by setting up a startup project.

I am using Visual Studio Code, where I am unable to set up a startup project.

Is there a way to set up an entry point for a C# project in Visual Studio Code?

like image 535
Prasad Telkikar Avatar asked Nov 07 '17 07:11

Prasad Telkikar


People also ask

How do you fix program has more than one entry point defined compile with main to specify the type that contains the entry point?

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.

How do I fix a program with more than one entry point defined in C#?

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. Save this answer. Show activity on this post.

What is the point of VS Code?

With support for hundreds of languages, VS Code helps you be instantly productive with syntax highlighting, bracket-matching, auto-indentation, box-selection, snippets, and more. Intuitive keyboard shortcuts, easy customization and community-contributed keyboard shortcut mappings let you navigate your code with ease.


1 Answers

If both entry points are in the same project, setting the startup project wouldn't do anything anyway. You need to set the startup object.

This can be done in the project properties dialog in a full version of Visual Studio (look for "Startup object" under Application), or in the .csproj file by setting Project/PropertyGroup/StartupObject:

<StartupObject>Example.Addition</StartupObject>

Alternatively consider using a single Main() entry point which takes a command-line argument.

like image 199
lc. Avatar answered Oct 19 '22 03:10

lc.