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.
Code in the Addition.cs file:
using System;
namespace Example
{
class Addition
{
static void Main(string[] args)
{
int sum = 3 + 2;
Console.WriteLine(sum);
}
}
}
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 run"
It fails with the above error.
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.
I am using Visual Studio Code, where I am unable to set up a startup project.
Is there a way to set up an entry point for a C# project in Visual Studio Code?
Compile with /main to specify the type that contains the entry point. A program can only have one Main method. To resolve this error, you can either delete all Main methods in your code, except one, or you can use the StartupObject compiler option to specify which Main method you want to use.
There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point. Save this answer. Show activity on this post.
With support for hundreds of languages, VS Code helps you be instantly productive with syntax highlighting, bracket-matching, auto-indentation, box-selection, snippets, and more. Intuitive keyboard shortcuts, easy customization and community-contributed keyboard shortcut mappings let you navigate your code with ease.
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.
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>
Alternatively consider using a single Main()
entry point which takes a command-line argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With