问题
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