C# in VS Code

Build, run and debug C# in VS Code on Windows, macOS or Linux, with the .NET SDK, Microsoft's C# extension and a launch.json that tells the debugger what to start.

Apprentice · Software Development · C# · Updated

By

After this lesson you can

  • Explain what VS Code needs to build, run and debug C#, and find a C# project's Explorer, Problems panel and terminal
  • Write a launch.json and a tasks.json that build a C# console app and start it in VS Code's debugger
  • Debug a C# program in VS Code with breakpoints, stepping and the Variables, Watch and Call Stack sections

Key takeaways

  • This course uses Visual Studio on Windows. VS Code runs C# on Windows, macOS and Linux, which helps when you work on a Mac or Linux computer.
  • VS Code needs three things for C#. The .NET SDK builds and runs your code, VS Code is the editor, and Microsoft's C# extension adds C# smarts and a debugger.
  • Open the project's folder. The Explorer lists its files, the Problems panel lists build errors, and the terminal runs dotnet commands.
  • .vscode/launch.json says what the debugger starts (type coreclr and the path to your .dll). .vscode/tasks.json says how to build it first.
  • The Run and Debug view shows Variables, Watch, Call Stack and Breakpoints while the program is paused. F5, F10, F11 and Shift+F11 work as in Visual Studio.

This course builds C# in Visual Studio on Windows, and you can follow every lesson that way. This lesson is optional. Read it if you use a Mac or a Linux computer, where Visual Studio doesn't run, or if a class or a job hands you a C# project and opens it in VS Code. It shows how to build, run and debug C# in VS Code. The debugger here works the way the one in Visual Studio does, so the habits you learned there carry over.

You met VS Code in the lesson VS Code: a free code editor that you grow with extensions. Out of the box, it doesn't know C#. By the end of this lesson, it will build your program, run it, and stop on a breakpoint.

What VS Code needs for C#

Think of a workshop. VS Code is the workbench: a good place to work, but it can't build anything by itself. For C#, it needs two more things on the bench:

Piece What it does Without it
The .NET SDK Builds and runs C# programs: the dotnet command Nothing builds, in VS Code or anywhere else
VS Code The editor, the Explorer, the terminal, the debugger's windows You'd write code in a plain text editor
The C# extension (by Microsoft, id ms-dotnettools.csharp) Colors and checks your C# as you type, finds errors, and brings the .NET debugger VS Code shows C# as plain text and can't debug it

You may also see C# Dev Kit (ms-dotnettools.csdevkit), which adds a Solution Explorer like Visual Studio's and a test runner. It's optional, it has license terms of its own, and it asks you to sign in. Everything in this lesson needs only the C# extension.

Install the pieces

Install the .NET 10 SDK first, then VS Code, then the extension. Pick your computer:

Computer The .NET 10 SDK VS Code
Windows Already installed if you did Setup: Install the .NET 10 SDK (winget install -e --id Microsoft.DotNet.SDK.10) Already installed if you did Setup: Install VS Code
macOS Download the .NET 10 SDK installer from Microsoft's download page, https://dotnet.microsoft.com/download/dotnet/10.0: Arm64 for a Mac with Apple silicon (M1 and later), x64 for an older Intel Mac. Open it and follow its steps. brew install --cask visual-studio-code with Homebrew, or the download at https://code.visualstudio.com
Linux (Ubuntu 24.04 or later) sudo apt-get update && sudo apt-get install -y dotnet-sdk-10.0 The .deb download at https://code.visualstudio.com

Close your terminal and open a new one, so it finds the new dotnet command, then check it:

dotnet --version

It prints a version that starts with 10.0, such as 10.0.403.

Then add the extension: open VS Code, open the Extensions view (Ctrl+Shift+X; on a Mac, Cmd+Shift+X), search for C#, check that the publisher under it is Microsoft, and click Install.

A C# project in VS Code

Make a console project from the terminal, and open its folder in VS Code:

dotnet new console -o DebuggerTry
cd DebuggerTry
code .

dotnet new console makes the same kind of project Visual Studio's Console App does. -o DebuggerTry puts it in a new folder of that name. Trust the folder when VS Code asks. Then open Program.cs from the Explorer and replace its code with the program from Visual Studio's Call Stack step:

PrintFinalScore("Georgia", 12, 14, 6, 7);

void PrintFinalScore(string team, int q1, int q2, int q3, int q4)
{
    int finalScore = AddQuarters(q1, q2, q3, q4);
    Console.WriteLine(team + ": " + finalScore);
}

int AddQuarters(int q1, int q2, int q3, int q4)
{
    return q1 + q2 + q3 + q4;
}
Georgia: 39

Here are the parts of VS Code you'll use for C#. Most have a twin in Visual Studio:

Part Where What it's for In Visual Studio
Explorer (Ctrl+Shift+E) Side bar, left The folder's files: DebuggerTry.csproj, Program.cs, and the bin and obj folders a build makes Solution Explorer
Problems panel (Ctrl+Shift+M) Panel, bottom Every error and warning the C# extension finds, with its file and line. Click one to jump to it Error List
Terminal (Ctrl+`) Panel, bottom Runs dotnet build and dotnet run in the project's folder The Terminal window (View > Terminal)
Run and Debug view (Ctrl+Shift+D) Side bar, left Starts the debugger and shows Variables, Watch, Call Stack and Breakpoints The Locals, Watch and Call Stack windows

VS Code with the DebuggerTry folder open: the Explorer on the left lists DebuggerTry.csproj and Program.cs, the editor in the middle shows Program.cs, and the terminal panel at the bottom shows dotnet run printing Georgia: 39

To run the program, type dotnet run in the terminal. It builds the project, then runs it, and Georgia: 39 appears in the terminal. A red squiggle under your code is the C# extension pointing at an error; the Problems panel lists all of them.

Tell the debugger what to start: launch.json

Press F5 now, and VS Code may ask which debugger to use, or which project to start, before it does anything. Visual Studio knew because a solution says which project to start. VS Code needs that written down, in a file.

That file is launch.json, in a folder named .vscode inside your project's folder. It holds launch configurations: recipes the debugger follows when you press F5. A second file beside it, tasks.json, holds the build step that runs first, so the debugger always starts your latest code.

In the Explorer, make a folder named .vscode (with the dot) in the DebuggerTry folder. In it, make tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "command": "dotnet",
      "type": "process",
      "args": ["build", "${workspaceFolder}/DebuggerTry.csproj"],
      "problemMatcher": "$msCompile"
    }
  ]
}

Then launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch DebuggerTry",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "${workspaceFolder}/bin/Debug/net10.0/DebuggerTry.dll",
      "args": [],
      "cwd": "${workspaceFolder}",
      "console": "integratedTerminal",
      "stopAtEntry": false
    }
  ]
}

Read it one line at a time:

Property Here What it means
name Launch DebuggerTry What the Run and Debug view calls this configuration
type coreclr Which debugger: the .NET debugger the C# extension brings
request launch Start the program (the other choice, attach, joins one that's already running)
preLaunchTask build Run the task labeled build in tasks.json first. The two names must match exactly
program ${workspaceFolder}/bin/Debug/net10.0/DebuggerTry.dll The built program to start. ${workspaceFolder} is read "workspace folder": the folder you opened. net10.0 must match the <TargetFramework> in your .csproj
args [] Values to hand the program when it starts; none here
cwd ${workspaceFolder} The folder the program runs in ("current working directory"), where it looks for files it opens
console integratedTerminal Where the program's output goes: VS Code's terminal panel, which can also read what you type for Console.ReadLine(). The other choices are internalConsole (the Debug Console panel) and externalTerminal (a separate window)
stopAtEntry false Don't pause on the first line; pause only at your breakpoints

F5 starts launch.json's configuration, Launch DebuggerTry; its preLaunchTask, build, runs the task labeled build in tasks.json, which runs dotnet build; the build makes bin/Debug/net10.0/DebuggerTry.dll, and the debugger starts that file, named in launch.json's program line

The .dll file is your compiled program, the file dotnet build writes. The program line has to point at it exactly, which is why the project's name and the net10.0 folder are in the path. Open DebuggerTry.csproj and look for <TargetFramework>net10.0</TargetFramework>: that's the folder's name. With the .NET 10 SDK, a new project says net10.0.

Debugging in VS Code

Now F5 knows what to do. The steps are the ones from Visual Studio, and the keys are too:

  1. Click in the margin left of the line return q1 + q2 + q3 + q4;. A red dot appears: a breakpoint. (F9 adds one on the line your cursor is on.)
  2. Press F5. VS Code runs the build task, starts the program, and pauses on that line. A yellow arrow and a highlight show where it is.
  3. Look at the Run and Debug view on the left. Its sections fill in while the program is paused:
    • Variables: every variable here and its value: q1 is 12, q2 is 14, q3 is 6, q4 is 7.
    • Watch: click + and type an expression, such as q1 + q2. VS Code shows its value, 26, and keeps it up to date as you step.
    • Call Stack: the methods running now, newest on top: AddQuarters, then PrintFinalScore, then the top-level code. VS Code may add the class's name and other characters around each one.
    • Breakpoints: every breakpoint you've set, each with a box to turn it off without deleting it.
  4. Press Shift+F11 (Step Out) to finish AddQuarters and pause back in PrintFinalScore. Now Variables shows finalScore with 39 once its line has run (press F10 once more if it still says 0).
  5. Press F5 to continue. Georgia: 39 appears in the terminal panel, and the program ends.

The debug toolbar at the top of the window holds the same commands as buttons:

Key Button What it does
F5 Continue Runs until the next breakpoint, or to the end
F10 Step Over Runs the current line, then pauses on the next one
F11 Step Into Like Step Over, but goes inside a method the line calls
Shift+F11 Step Out Finishes the method you're in and pauses where it was called
Ctrl+Shift+F5 Restart Stops the program and starts it again from the top
Shift+F5 Stop Stops the program

VS Code paused on the return line in AddQuarters, with the Run and Debug view on the left showing Variables with q1 to q4 as 12, 14, 6 and 7, a Watch expression q1 + q2 showing 26, and the Call Stack listing AddQuarters above PrintFinalScore

When F5 doesn't work

What you see What's wrong What to do
VS Code asks which debugger to use There's no launch.json, or it isn't in a folder named .vscode Check the folder's name, dot included, and that it sits inside the project's folder
VS Code says it can't find the task build preLaunchTask names a task tasks.json doesn't have Make the label in tasks.json and preLaunchTask the same word
VS Code says the program file doesn't exist The program path doesn't match the .dll the build made Check the project's name and the net10.0 part against your .csproj, and look in the bin/Debug folder
dotnet isn't a command the terminal knows The terminal started before the SDK was installed Close the terminal and open a new one
The build fails, and the program never starts Your code has an error Open the Problems panel, fix the first error, and press F5 again

Visual Studio or VS Code?

Visual Studio VS Code
Runs on Windows Windows, macOS and Linux
For C# out of the box Yes With the .NET SDK and the C# extension
Debugger setup Built in: it starts the startup project A launch.json and a tasks.json in .vscode
This course's lessons and exercises Written for it They build and run the same way: the projects are the same .csproj files

In this course, stay with Visual Studio if you're on Windows. If you're on a Mac or Linux, open each exercise's folder in VS Code, run its tests with dotnet test in the terminal, and give each project its own launch.json when you want the debugger: the name of the project and of its .dll change, the rest stays.

Check yourself

2 questions to try on the lesson page.

So what

VS Code can build, run and debug the same C# projects Visual Studio does, on any computer. It needs the .NET SDK, the C# extension, and a launch.json that tells the debugger what to start.

Key terms