Configure debugging for Visual Studio Code for a C# project

- 3 mins

This post was heavily inspired by a similar article Debugging C# on OS X with Visual Studio Code by Nate Woolls

For the past two weeks, I’ve been learning C# and 2D game development with Unity while working through the Unity3D Rogulike tutorial. To edit C# scripts, I use Visual Studio Code.

In order to practice C# basics, I decided to configure my installation of Visual Studio Code to debug C# scripts. I found a useful tutorial (which I mentioned at the top of this article) and was able to configure Visual Studio Code successfully. I want to outline all the steps I took, providing as much detail as I can.

  1. Install mono for OSX. Mono is a cross-platform, open-source .NET framework and is required by Visual Studio Code.
  2. Install Visual Studio Code
  3. Install C# support. You can install it from within VS Code by searching for ‘C#’ in the Extensions: Install Extension dropdown (type Cmd + Shift + P then type ‘ext install’)
  4. Install ASP.NET 5 for Mac OSX
  5. Install DNX. DNX is the .NET Execution Environment (DNX) used to build and run .NET projects.
  6. Install Yeoman. Yeoman is a scaffolding tool for modern web apps. The installation command is: npm install -g yo This step assumes you already have NodeJS installed

  7. Once Yeoman has been installed, install the ASP.NET generator. The generator will be used to create our sample console application. The generator can be installed with the command: npm install -g generator-aspnet

  8. Execute yo aspnet to create the sample application in the current directory. Choose the 'Console Application' option:

  9. Once the project has been created, navigate into the project’s directory and execute dnu restore. From the documentation: > The restore command will look at the dependencies of your application and download them, adding them to your apps packages directory. It downloads the entire graph of dependencies, even though you only explicitly declare the top level things that you directly depend on.

  10. Launch Visual Studio Code & open the project’s folder. Click Debug button followed by the ‘Open launch.json’ button in the top-left corner of the editor.

  11. Choose ‘C# Mono’:

  12. Use this configuration in the launch.json file:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Launch console application",
          "type": "mono",
          "request": "launch",
          "preLaunchTask": "exe",
          "program": "${workspaceRoot}/Program.exe",
          "args": [],
          "cwd": "${workspaceRoot}",
          "stopAtEntry": false
        }
      ]
    }
    
  13. Open the command palette (Cmd + Shift + P) and choose Configure Task Runner.

  14. Use this configuration in the tasks.json file:

    {
        "version": "0.1.0",
        "command": "mcs",
        "args": [
            "-debug",
            "Program.cs"
        ],  
        "showOutput": "silent",
        "taskSelector": "/t:",
        "tasks": [
            {
                "taskName": "exe",
                "isBuildCommand": true,
                "problemMatcher": "$msCompile"
            }
        ]
    }
    
  15. Open the command palette and choose Build Task.

  16. Click the Debug button followed by the ‘Play’ button to execute the script file. You should be able to view a screen similar to this:

Ifeanyi Oraelosi

Ifeanyi Oraelosi

Making stuff to facilitate learning and creativity. Also, video game experiments.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium vimeo