# How to Run C# in VSCode (and Compile, Debug, and Create a Project)

While many developers use Visual Studio for C# development, I still use VSCode. In this post, I'll show you how to run C# in VSCode, as well as create a project, execute the code, debug, and compile it.

As stated above, I make it a point to use VSCode for as many things as I can get away with.

I feel comfortable in VSCode, know the shortcuts, and just don't like jumping editors.

I started out with C# in Visual Studio because I was told to. It's easy, has many, many features, and is what the tutorials told me to use.

Later, I made it a point to become efficient in it with VSCode. I was using it for everything else. Why not with C#? Visual Studio, to me, is just too bulky and has too much going on.

So in this post, I want to help you do the same.

Whether you are just starting out in C# or have decided to switch from Visual Studio to VSCode, I'm going to share with you how to run, debug, execute, and compile your C# code in VSCode.

<AnnouncementBox variant="recommendation" title="Learning C#? Here are my recommendations">
  <p>If you’re learning C#, here are my TOP course recommendations:</p>
  <ul>
    <li>
      <strong>Beginner →</strong>
      <a href="https://geni.us/NjLkiAD" target="_blank" rel="noopener">
        C# Basics for Beginners: Learn C# Fundamentals by Coding
      </a>
      — master C# fundamentals
    </li>
    <li>
      <strong>Intermediate →</strong>
      <a href="https://geni.us/9A02OJT" target="_blank" rel="noopener">
        C# Intermediate: Classes, Interfaces and OOP
      </a>
      — classes, interfaces and object-oriented programming (OOP)
    </li>
    <li>
      <strong>Advanced →</strong>
      <a href="https://geni.us/rQsZn" target="_blank" rel="noopener">
        C# Advanced Topics: Prepare for Technical Interviews
      </a>
      — events, delegates, lambda expressions, linq, async/await and more!
    </li>
  </ul>
</AnnouncementBox>

<h3 id="20210901-video">Watch the Video?</h3>

Before we get started, if you prefer video format, check out the tutorial on YouTube.

<YouTube id="https://www.youtube.com/embed/DAsyjpqhDp4" />

<br />
Otherwise, continue reading.

<h2 id="20210929-how">How to Run C# in VSCode</h2>

<h3 id="20210929-install">1. Install the .NET SDK</h3>

First, download and install the .NET SDK. As of writing, the latest LTS version is **.NET 8**.
Download here: [https://dotnet.microsoft.com/en-us/download](https://dotnet.microsoft.com/en-us/download)

Then confirm that dotnet is installed by checking the version in a terminal window:

```bash
dotnet --version
# 8.0.414 (or whatever version yours is)
```

Also go ahead and install <a href="https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp" target="_blank" aria-label="link to the vs code extension">this C# VSCode extension</a>.

<h3 id="20210929-create">2. Create a new C# project in VSCode</h3>

Next, create a new project and open it in VSCode:

```bash
dotnet new console -o app
cd app
code . # to open project in VSCode
```

This will generate a `Program.cs` file.

In .NET 6+, the file uses **top-level statements** by default meaning you will only see this line ([explanation](https://learn.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates)).:

```csharp
Console.WriteLine("Hello, World!");
```

That’s it! No namespace, class, or `Main` method needed.

💡 If you prefer the old explicit Program.cs with `Main`, you can add the flag:

```bash
dotnet new console --use-program-main
```

<h3 id="20210929-run">3. Run Your C# Code in VSCode</h3>

To execute your code, simply run:

```bash
dotnet run
```

...and you should see `Hello World` displayed in the console.

> 💡 Bonus: Try Hot Reload with: `dotnet watch run`. This will recompile and reload changes as you edit the file. No need to stop and restart.

<h3 id="20210929-debug">4. Debug Your C# Code in VSCode</h3>

First, be sure you installed the official C# extension mentioned above. If not, it can be found <a href="https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp" target="_blank">here</a>.

Next, in VSCode open the Command Palette by going to View > Command Palette (or use the shortcut if you know what it is), and search for `.NET: Generate Assets for Build and Debug`

<img data-rjs="2" src="https://travismedia.gumlet.io/public/images/2021/09/NET-debugging-in-vscode.jpeg" />

Choosing this will generate a .vscode folder with a prepopulated build configuration in it.

Now go to the "Run and Debug" tab in VSCode, set your breakpoint(s), and click the Play button to debug.

<Image src={vscodeDebugging} alt="debugging image in vs code" />

<h3 id="20210929-compile">5. Compile Your Code</h3>

To compile your code, run:

```bash
dotnet build
```

After that is done you will have an executable (exe or dll) in your `/bin` folder. Depending on your build configuration it may be in a **Debug** folder or a **Release** folder.

<h3 id="20210929-conclusion">Conclusion</h3>

And that's how you run C# code in VSCode.

Now build our your code, debug it as needed, run `dotnet run` to execute your code, and `dotnet build` to compile it.

Rinse and repeat.

Let me know below if you have any questions.