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 TrottC# ASP.Net MVC • November 22, 2019
How to Crete a .NET Console Application Using Visual Studio Code

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 building a console application using visual studio code.

This article will use Visual Studio Code, a free code editor for Windows, Linux, and macOS that Microsoft created. It's a desktop source code editor that operates on Windows, macOS, and Linux, and it's designed to be small yet powerful, making it easy for you to get started with .NET console application development.

To get started:

  1. Download Visual Studio Code from the official website if you haven't already done so. You'll also need the .Net Core SDK.
  2. Once you've installed Visual Studio Code, open it and select the Extensions icon from the panel.
  3. Look for and install the 'Microsoft C# for Visual Studio Code' package. This will install the necessary files to create .Net Core Console Applications within Visual Studio Code.

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

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

First, we must create a folder on the hard drive to store our solution files. I will use C:\Dev\ConsoleDemo\, but you can use anything.

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 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, run dotnet restore command in the terminal to resolve the project's dependencies and give you access to the required .NET Core packages 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 and more complicated processes like connecting with a database or online service. Visual Studio Code includes a powerful code editor with syntax highlighting, code completion, and debugging tools to assist you in writing and testing code. Use these tools to ensure 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 your project's directory. 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.

Usually, 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. You can quickly identify and fix any issues in your .NET console application using the debugger.

In Visual Studio Code, click the debug button on the left-hand menu to access the debug tools. Like 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 like 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 a quick introduction to creating 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.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. 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?

New comments for this post are currently closed.