C#  

VS Code for C# Developers: Create, Run, and Debug Apps

Introduction

With Microsoft's retirement of Visual Studio for Mac on August 31, 2024, developers are encouraged to transition to alternative tools. Visual Studio Code (VS Code), combined with the C# Dev Kit, offers a robust, cross-platform environment for C# development on macOS. Microsoft learn

So, given that this is our step-by-step approach.

  • Setting up VS Code for C# development
  • Creating and running a simple C# console application
  • Check if we can debug within VS Code

Prerequisites

Ensure the following are installed on your system.

  1. .NET SDK: Download and install the latest .NET SDK from the .NET official website.
  2. Visual Studio Code: Install VS Code from the official website.
  3. C# Extension for VS Code: Install the C# extension by Microsoft:
    • Open VS Code.
    • Navigate to the Extensions view by clicking on the Extensions icon in the Activity Bar on the left side of the window.
    • Search for "C#" and install the extension provided by Microsoft.
      Microsoft

VS Code Extensions view highlighting the C# extension.

Create a new C# Console Application

Step 1. Open Terminal

Launch a terminal window from your preferred working location. This can be your home directory or any project folder you like to work in.

Step 2. Navigate to Your Workspace

Move to your favorite place for coding projects.

cd /Users/rikam/Projects

Step 3. Create a New Folder and Initialize the Project

Use the following commands to create a new directory for your app and generate a basic C# console application.

mkdir Leetcode
cd Leetcode
dotnet new console

This will scaffold a new project with a Program.cs file and required project configuration (.csproj file).

To open your C# project in Visual Studio Code from the terminal, use this command.

  • code: This is the command to launch VS Code.
  • This tells VS Code to open the current directory (your project folder).
code .

If the code command is not recognized

You might need to install the code command in your shell.

Use the Menu Bar

You can access the Command Palette manually.

  • Click on View in the top menu bar.
  • Select Command Palette, then type and select: "Shell Command: Install 'code' command in PATH"
    Command Palette

Command Palette with "Shell Command: Install 'code' command in PATH" highlighted.

Now, you can open any folder in VS Code using the (code .) command

Let's write some C#

Open Program.cs: In the Explorer view, open the Program.cs file, and let's add a simple function to print numbers. We are initializing an array of five integers and printing each to the console.

void PrintNumbers()
{
    int[] numbers = { 10, 20, 30, 40, 50 };
    foreach (int number in numbers)
    {
        Console.WriteLine(number);
    }
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}
PrintNumbers();

Run the App

Build and Run

// Building the app
dotnet build

// Running the app
dotnet run

The console should display.

10
20
30
40
50

Debugging the Application

Step 1. Add a Breakpoint

Click in the gutter to the left of the line Console.WriteLine(number) to add a breakpoint.

Debugging

The Red dot is a breakpoint set on line 6.

Step 2. Start Debugging

Press F5 or navigate to Run > Start Debugging. VS Code may prompt you to select the environment for your project. For a C# app, you'll choose ( .NET or .NET Core, depending on your version )

Navigate to Run

After this selection, VS Code automatically generates a .vscode/launch.json file in your project folder. This file contains configuration instructions that tell VS Code how to build and run your app.

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/bin/Debug/net8.0/Leetcode.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

Next, the debugger will start, and execution will pause at the breakpoint.

Inspect Variables

  • Use the Debug pane to inspect the value of the number and other variables.
  • Use the Step Over (F10) and Step Into (F11) commands to navigate through the code.
    Command

Conclusion

Transitioning from Visual Studio to Visual Studio Code ensures continued support and access to modern development tools for C#. With the C# extension, VS Code provides a streamlined environment for building and debugging .NET applications.