Visual Studio代码:程序具有多个定义的入口点 [英] Visual Studio Code: Program has more than one entry point defined

查看:568
本文介绍了Visual Studio代码:程序具有多个定义的入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Visual Studio代码 .该项目包含两个.cs文件,分别是 Addition.cs Substraction.cs .这两个文件都包含 main()函数,并且两个文件都包含两个不同的程序.

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.

代码:

using System;

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

Substraction.cs 文件中的

代码

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运行"

"dotnet run"

由于上述错误而失败.

我知道,因为同一项目中有两个main()函数(入口点)会导致此错误,但这可以在Visual Studio中通过设置启动项目来克服.

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

我正在使用 Visual Studio代码,在这里我无法设置启动项目.

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

是否可以在Visual Studio Code中为C#项目设置入口点?

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

推荐答案

如果两个入口点都在同一个项目中,则设置启动项目将无济于事.您需要设置启动 object .

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.

这可以在完整版本的Visual Studio中的项目属性对话框(在应用程序"下查找启动对象")中完成,也可以通过设置Project/PropertyGroup/StartupObject在.csproj文件中完成:

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>

或者考虑使用一个带有命令行参数的Main()入口点.

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

这篇关于Visual Studio代码:程序具有多个定义的入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆