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:

  1. .Net Core
  2. VS Code

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.

dotnet --version
dotnetnew — v

You should be getting the version installed depending on which version you installed:

Output:
2.0.0

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.

Step 1:

Open a Command prompt or a Terminal in VS Code (by clicking Ctrl+` ) and enter the below command

c:\> dotnet new console -o "ConsoleApplication"

1. dotnet new console : scaffolds the console application template.

2. -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.

Step 2:

Switch the path to the folder ConsoleApplication using the below command.

c:\> cd ConsoleApplication

Step 3:

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.

Step 4:

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.

  • Task.json
  • Launch.json

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:

"program": "${workspaceRoot}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",

With:

"program": "${workspaceRoot}/bin/Debug/netcoreapp2.0/ConsoleApplication.dll",

Now you are all set to debug your Console application in Visual Studio Code

Step 5:

  • 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.

Step 6:

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.

Happy Coding!!