Visual Studio Code:“程序定义了多个入口点"

编程入门 行业动态 更新时间:2024-10-26 14:38:12
本文介绍了Visual Studio Code:“程序定义了多个入口点"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个 C# 项目使用 Visual Studio 代码.该项目包含两个 .cs 文件,Addition.cs 和 Subtraction.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 VisualStudio (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:“程序定义了多个入口点"

本文发布于:2023-11-16 10:44:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   入口   定义   程序   Studio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!