Tuesday, 13 May 2025

C# Developers Can Create, Run, and Debug Apps with Visual Studio Code

Leave a Comment

 Developers are urged to switch to other tools as Microsoft plans to remove Visual Studio for Mac on August 31, 2024. For C# programming on macOS, Visual Studio Code (VS Code) and the C# Dev Kit provide a stable, cross-platform environment.

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.

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

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 )

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

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.

ASP.NET Core 9.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 9.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.



0 comments:

Post a Comment