Visual Studio Code:“程序定义了多个入口点" [英] Visual Studio Code: "Program has more than one entry point defined"

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

问题描述

我创建了一个 C# 项目使用 <强>Visual Studio 代码.该项目包含两个 .cs 文件,Addition.csSubtraction.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.

Addition.cs 文件中的代码:

using System;

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

Subtraction.cs 文件中的代码

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

由于上述错误而失败.

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

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.

我正在使用 Visual Studio Code,但无法设置启动项目.

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

有没有办法在 Visual Studio Code 中为 C# 项目设置入口点?

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

推荐答案

如果两个入口点在同一个项目中,那么设置启动项目无论如何都没有任何作用.您需要设置启动对象.

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 的项目属性对话框中完成(在应用程序下查找启动对象"),或者在 .csproj 文件中通过设置 Project/PropertyGroup/StartupObject:

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 Code:“程序定义了多个入口点"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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