我创建了一个 C#项目, strong> 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.
Addition.cs 文件中的代码:
using System; namespace Example { class Addition { static void Main(string[] args) { int sum = 3 + 2; Console.WriteLine(sum); } } }Substraction.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 run
"dotnet run"
上面的错误。
我知道由于同一项目中的两个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代码,在这里我无法设置启动项目。
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?
推荐答案如果两个入口点都在同一项目,设置启动项目将无济于事。您需要设置启动 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中的项目属性对话框中完成(查找或在.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代码:“程序定义了多个入口点”
发布评论