Visual studio code: Program has more than one entry point defined?

穿精又带淫゛_ 提交于 2019-12-18 03:44:23

问题


I created C# project using visual studio code. This project contains two .cs files Addition.cs and Substraction.cs both files contains main() function, Both file contains two different programs.

Code in Addition.cs file

using System;

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

Code in 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 program one by one, but when I do

"dotnet run"

It fails with above error.

I know because of two main() functions(Entry points) in same project creating this error, but this can be overcome in visual studio by setting up startup project.

I am using Visual studio code, where I am unable to setup a startup project. Is there any way to setup entry point for c# project in visual studio code?


回答1:


If both entry points are in the same project, setting the startup project wouldn't do anything anyway. What you need to do is set the startup object. This can be done in the project properties dialog in full VS (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.



来源:https://stackoverflow.com/questions/47152454/visual-studio-code-program-has-more-than-one-entry-point-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!