Get Started with C# and Visual Studio Code

.NET Core gives you a fast and modular platform for creating applications that run on Windows, Linux, and macOS. Use Visual Studio Code with the C# extension to get a powerful editing experience with full support for C# IntelliSense (smart code completion) and debugging.

Prerequisites

  1. Install Visual Studio Code.
  2. Install the .NET Core SDK.
  3. Install the C# extension from the Visual Studio Code Marketplace.

Hello World

Let's get started with a simple "Hello World" program on .NET Core:

  1. Open a project:

    • Open Visual Studio Code.
    • Click on the Explorer icon on the left menu and then click Open Folder.
    • Select File > Open Folder from the main menu to open the folder you want your C# project to be in and click Select Folder. For our example, we're creating a folder for our project named HelloWorld.

      VSCodeOpenFolder

  2. Initialize a C# project:

    • Open the Integrated Terminal from Visual Studio Code by selecting View > Integrated Terminal from the main menu.
    • In the terminal window, type dotnet new console.
    • This command creates a Program.cs file in your folder with a simple "Hello World" program already written, along with a C# project file named HelloWorld.csproj.

      The dotnet new command

  3. Resolve the build assets:

    • For .NET Core 1.x, type dotnet restore. Running dotnet restore gives you access to the required .NET Core packages that are needed to build your project.

      The dotnet restore command

      Note

      Starting with .NET Core 2.0, you don't have to run dotnet restore because it's run implicitly as part of dotnet build or dotnet run. It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Visual Studio Team Services.

  4. Run the "Hello World" program:

    • Type dotnet run.

      The dotnet run command

You can also watch a short video tutorial for further setup help on Windows, macOS, or Linux.

Debug

  1. Open Program.cs by clicking on it. The first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor.

    Open the Program.cs file

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

    Prompt for missing assets

  3. To open the Debug view, click on the Debugging icon on the left side menu.

    Open the Debug tab

  4. Locate the green arrow at the top of the pane. Make sure the drop-down next to it has .NET Core Launch (console) selected.

    Selecting .NET Core

  5. Add a breakpoint to your project by clicking on the editor margin, which is the space on the left of the line numbers in the editor, next to line 9.

    Setting a Breakpoint

  6. To start debugging, select F5 or the green arrow. The debugger stops execution of your program when it reaches the breakpoint you set in the previous step.

    • While debugging, you can view your local variables in the top left pane or use the debug console.

    Run and Debug

  7. Select the green arrow at the top to continue debugging, or select the red square at the top to stop.

Tip

For more information and troubleshooting tips on .NET Core debugging with OmniSharp in Visual Studio Code, see Instructions for setting up the .NET Core debugger.

See also

Setting up Visual Studio Code
Debugging in Visual Studio Code