How to create a Console application in .Net Core and Visual Studio Code(Vs Code)
If you are used to Visual Studio for creating the new projects (Console,Windows,WPF,Web..)using the templates provided then you might feel held back when you are trying to use the new Visual Studio Code to create the new project as we don’t have the Menu to create one.
If you have encountered the above thought then don’t worry you are not alone 😉.
Let’s briefly take a look at how to create one.
We would require the following installed:
Once installed we can check to make sure that .Net Core is installed and the path variable is set properly be executing the below command in the command prompt.
You should be getting the version installed depending on which version you installed:
Since we have set up the necessary tools required let’s begin with creating the console application.
Before we actually start with scaffolding the console application template. Let’s try to understand briefly the tool which helps in Scaffolding, Debugging and Building the .Net Core Application — .Net CLI Tool.
In simple terms it’s a cross platform tool which aids in developing the .Net Applications and other high level tools.
Command prompt or a
Terminal in VS Code (by clicking
Ctrl+` ) and enter the below command
c:\> dotnet new console -o "ConsoleApplication"
dotnet new console : scaffolds the console application template.
-o "ConsoleApplication" : inputs the folder in which to scaffold the files into.
Now you will have all the files created required for the Console Application.
Switch the path to the folder
ConsoleApplication using the below command.
c:\> cd ConsoleApplication
Enter the below command to open the ConsoleApplication project in VS Code
c:\ConsoleApplication> code .
This will open the ConsoleApplication project in the Visual Studio code. VS Code will prompt you to install the C# extension rightly identifying the project type which you have opened.
Go ahead and install the C# extension from Microsoft which provides the great development and productivity experience like Syntax Highlighting, Intellisense, Debugging Support and many more.
Configure VS Code to build and debug the project.
We need to configure the VS code to tell it how to compile and debug the applications. For this it uses 2 files.
Usually VS Code will prompt you to add the missing assets for debug and build automatically when you open any project and if you have C# extension installed.
We will not be discussing on how to add the required files manually to build and debug the project here. you can find more details on this over here.
Open launch.json file and find the section
"name": ".NET Core Launch (console)"
Replace the belwo line:
Now you are all set to debug your Console application in Visual Studio Code
- Place a break point in program.cs file either by clicking on the line of code where you want the debugger to break and pressing F9 key or by clicking on the space to the left of the line number.
- Press F5 to begin debug your code.
You will see the output
Hello world! in the debug console.
Hope this short article helped to build and debug the Console application in Visual Studio Code.