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

10,065

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.

Share:
10,065
Prasad Telkikar
Author by

Prasad Telkikar

Email: telkikar[dot]prasad[at]gmail[dot]com If you can't explain it simply, you don't understand well enough. - Albert Einstein

Updated on July 23, 2022

Comments

  • Prasad Telkikar
    Prasad Telkikar almost 2 years

    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?

  • Prasad Telkikar
    Prasad Telkikar over 2 years
    Hi John, Thanks for your answer but it looks like your answer is conveying same thing what @Ic. is provided in his/her answer
  • John Zampa
    John Zampa over 2 years
    Yes, it is. But he hasn't mentioned where to put this code when you are using Visual Studio Code in the .csproj file. I have written this for absolute beginners.