Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
475 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...