다음을 통해 공유


Visual Studio Code: Create C# Console Application And Debug

Introduction

 

In this article, we explore the Visual Studio Code is a free code editor developed by Microsoft for Windows, Linux, and macOS. We create the C# Console application in Vs  code with the help of .NET Core.

 

Visual Studio Code (Vs Code) is optimized for cross-platform .NET Core development.  Visual Studio Code is a lightweight but powerful source code editor which runs on your 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.

 

Prerequisites

  1. Install Visual Studio Code.
  2. Install the .NET Core SDK.

Objective

 

Let's get started with a simple "Console demo application" program on .NET Core,

 

Create Console Application

 

Follow the below steps to create the console application,

 

Steps

  1. Open a project -> Open Visual Studio Code.

  2. Open the Integrated Terminal from Visual Studio Code selecting 

  3. Terminal > New Terminal from the sub menu.

  4. The **dotnet **command creates a new application of type console for you. The -o parameter creates a directory named ConsoleApplicationDemo where your app is stored and populates it with the required files. The cd ConsoleApllicationDemo command puts you into the newly created app directory.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code.png

  1. Switch the path to the folder ConsoleApplicationdemo using the below command.

  2. D:\ cd ConsoleApplicationdemo

  3. Enter the below command to open the ConsoleApplication project in VS Code

  4. D:\ConsoleApplicationdemo> code.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code02.jpg

  5. Repeated  Step for opening the terminal command prompt.

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

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code03.jpg

  1. Type the following command: dotnet run. Run the console application and the below screen output appears.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code04.jpg

Debugging

 

One of the key features of VS Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile and debug loop.

 

Follow the below steps for  the C# debugger,

 

Steps

  1. The first time you open a C# Program.cs file in Visual Studio Code the below pop window appears

  2. Visual Studio Code should prompt you to add the missing assets to build and debug your app. Select Yes.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code05.png

  1. Start the install OmniSharp loads in the editor.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code06.png

  1. After completion the  below message shows in the command window

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code07.jpg

  2. VS Code left Navigation -- click on Debugger Icon

  3. Locate the https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code09.jpg green arrow at the top of the pane. Make sure the drop-down next to it has .NET Core https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code08.jpg Launch (console) selected.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code10.jpg

  4. Add a breakpoint to your project by clicking on the editor margin

  5. To start debugging, press F5 or select the green arrow https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code11.png

  6. Show the local variable value in the top left pane.

    https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code12.jpg 

Top Extensions

 

In the VS code there are more debugging extensions for debugging other languages and runtimes (including PHPC#PythonC++PowerShell and many others), look for Debuggers extensions in our VS Code Marketplace or select Install Additional Debuggers in the top-level Debug menu.

 

https://www.c-sharpcorner.com/article/how-to-create-a-c-sharp-console-application-and-debug-in-visual-studio-code/Images/How%20To%20Create%20A%20C%23%20Console%20Application%20And%20Debug%20In%20Visual%20Studio%20Code13.png