Home > Blockchain >  How do I set the default c# file in my Visual Studio Code c# Project?
How do I set the default c# file in my Visual Studio Code c# Project?

Time:05-08

I am new to using Visual Studio Code. I have always used Visual Studio IDE in Windows. I am trying to run a simple Hello World Program on Mac with VS Code. I ran the following command

dotnet new console

which successfully created the csProj file and Program.cs file with the code Console.WriteLine("Hello, World!"); Now by issuing dotnet run command I can get the output from this Program as well. But I have a different cs file called "Hello.cs" in the same Project location. How do I get the Hello.cs to run instead of the default "Program.cs" created by dotnet new console command. I tried giving the following property group in the .csproj file but VS Code reports error this is reserved property.

<PropertyGroup><StartupObject>Hello.HelloWorld</StartupObject></PropertyGroup>

Is there another way to choose your default startup CS File if you have multiple CS files in your Project.

CodePudding user response:

The entry point of your application will be the method called Main() in the default namespace of your project. If you want to change the entry point, you can rename your methods, but a better way would be to simply call the code you want to run from "Hello.cs" in your Main() method in "Program.cs."

CodePudding user response:

With using C# 9 Top-level statement feature you can have only one entry point. so you dont need select a default entry point. Remove all Main methods from other files and other top-level files if you want to use this feature.

Read more about Top-level statements...

  • Related