'How do I set the default c# file in my Visual Studio Code c# Project?
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.
Solution 1:[1]
dotnet new console command creates a new C# Console Project based on C# 10.0 . This creates a Program.cs file that has something called as a Top Level Statement without a main method. as shown below:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Notice that this does not have the default Main() method which is usually the starting entry point for CS Console Application Projects Once this statement is introduced in a .cs file in my csProject any other cs file I create and declare my classic entry point defined by my Main() method will be ignored for the purpose of Entrypoint. <<== This is a very important point to understand.
My Hello.cs file had the following code:
public static void Main()
{
Console.WriteLine("Hello World1");
}
This was ignored by C# 10.0 compiler. [From C# 9.0 onwards this is the behavior]. https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
Now All I had to do to make my Hello.CS Main() method my entry point to my C# Console Project is to get rid of the default top level entry point automatically created by the dotnet new console command.
Now I commented out this line in my Program.cs file
//Console.WriteLine("Hello, World!");
Rerun my Project [Command+Shift+D] and voila! I have my code in Hello.cs now executing.
Solution 2:[2]
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."
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prabhat K |
| Solution 2 | Garrett |
| Solution 3 |
