How to Crete a .NET Console Application Using Visual Studio Code

How to create a C# Console Application using Visual Studio Code and the .Net Core SDK. Visual Studio 2019/2022 not required.

By Tim Trott | C# ASP.Net MVC | November 22, 2019
880 words, estimated reading time 3 minutes.

You're in luck if you want to use Visual Studio Code to build a .NET console application. From setting up your development environment to creating and running your first program, this tutorial will walk you through every step of the procedure. This lesson has everything you need to get started with .NET programming, regardless of your level of programming experience.

In this article, we'll be using Visual Studio Code, a free code editor for Windows, Linux, and macOS that was created by Microsoft. It is a desktop source code editor that operates on Windows, macOS, and Linux and is small but effective.

You can download Visual Studio Code from this page if you don't already have it installed Download Visual Studio Code . You will also need the .Net Core SDK . Launch Visual Studio Code and select the Extensions icon from the panel, look for and install the "Microsoft C# for Visual Studio Code" package. The most recent version is v1.23.9 as of this writing. This will install the required files to create .Net Core Console Applications within Visual Studio Code.

How To Create a .NET Console Application using Visual Studio Code

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

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.

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"

Visual Studio Code will create a new folder with all the necessary files for your console application. You can then select from the menu bar 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?
Create a .NET Console Application using Visual Studio Code

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.

Visual Studio Code: Create C# Console Application And Debug
Visual Studio Code: Create C# Console Application And Debug

Writing Your Logic Code for the Task

After you've created your console application project, you can begin writing code to carry out the needed operations. This can contain simple calculations as well as more complicated processes such as connecting with a database or online service. To assist you in writing and testing code, Visual Studio Code includes a powerful code editor with features such as syntax highlighting, code completion, and debugging tools. Use these tools to ensure that your code is error-free and runs as expected.

Building and Running Your Application

It's time to build and launch your application now that you've written your code. You can do this in Visual Studio Code by opening the terminal and browsing the directory containing your project. Then, to create your application, use the command dotnet build. If there are no issues, you can execute your program with the command dotnet run. The output of your application should be seen in the terminal window. You have successfully constructed a .NET console application with Visual Studio Code!

Debugging Console Application using Visual Studio Code

One of the cool features of VS Code is its great debugging support. VS Code's built-in debugger helps accelerate your development lifecycle. Debugging and troubleshooting are important steps in the development process.

Normally when a program encounters an error the terminal window will display error messages that can help you identify the problem. The debugger allows you to step through your code line by line and see the values of variables at each step, which functions are called and the results of calculations. Using the debugger you can quickly identify and fix any issues in your.NET console application.

In Visual Studio Code, click the debug button on the left-hand menu to access the debug tools. Similar to Visual Studio 2019, add a breakpoint to your project by clicking on the editor margin in Program.cs. Press F5 or click the green arrow at the top of the debug window to begin debugging.

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

This has been just a quick introduction to how to create a console application using Visual Studio Code. For more information on C# programming starting with console applications please see our comprehensive Introduction to Programming with C# tutorial series.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.