Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > Create a .NET Console Application using Visual Studio Code

Published 22nd November 2019 by

This tutorial shows you how to create and run a .NET console application using Visual Studio Code and the .NET CLI.

In this article, we explore the Visual Studio Code is a free code editor developed by Microsoft for Windows, Linux, and macOS. It is a lightweight yet powerful source code editor which runs on desktop and is available for Windows, macOS, and Linux.

Use Visual Studio Code with the C# extension to get a powerful editing experience with full support for C# IntelliSense and debugging.

If you don't have Visual Studio Code installed you can get it here. You will also need the .Net Core SDK.

Creating a Console Application in VSCode

Let's get started and create a simple console application with Visual Studio Code and .NET Core.

So first we need to create a folder on the hard drive which will store our solution files. I'm going to use C:\Dev\ConsoleDemo\ but you can use anything you want.

Open up Visual Studio Code and click on extensions tab. Search for and install the Microsoft C# for Visual Studio Code extension. At the time of writing v1.23.9 is the latest version.

Next, open the Integrated PowerShell Terminal from Visual Studio Code by selecting Terminal >New Terminal or pressing Ctrl+Shift+'.

On the command line change the directory to C:\Dev and enter the following command:

dotnet new console -o "ConsoleDemo"

This will create the directory ConsoleDemo and install the VS project files. You can then select File >Open Folder and navigate to the directory just created.

The first time you open a C# Program.cs file in Visual Studio Code it should prompt you to add the missing assets to build and debug your app. Select Yes.

Required Assets to build and debug are missing. Add them?

Required Assets to build and debug are missing. Add them?

Next, in the terminal, run dotnet restore command to resolve the dependencies of the project and give you access to the required .NET Core packages that are needed to build your project.

You can now run dotnet run to run the console application and the below screen output appears.

Visual Studio Code: Create C# Console Application And Debug

Visual Studio Code: Create C# Console Application And Debug

Debugging Console Application in VSCode

One of the cool features of VS Code is its great debugging support. VS Code's built-in debugger helps accelerate your development lifecycle.

To access the debug tools click on the debug button on the left hand menu in Visual Studio Code. Add a breakpoint to your project by clicking on the editor margin in Program.cs, in the same way as you would in Visual Studio 2019. To start debugging, press F5 or select the green arrow at the top of the debug window.

Debugging Console Application in VSCode

Debugging Console Application in VSCode

Debugging works just like debugging in Visual Studio Professional, F10 steps over, F11 Steps into. F5 to continue running. For more information on using the debugger, including watches, locals and call stacks, see the article Error and Exception Handling in Visual Studio and check the heading "Using the Visual Studio Debugger".

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.