Building and running C# programs

To write and compile C# code, you need two things:

  1. An editor or IDE. If you don't already have a favorite, I recommend Visual Studio Code, which runs on all platforms and supports many languages including C#. Of course, many other editors exist as well (e.g. Geany, Sublime, Atom).

  2. An implementation of the C# compiler and runtime libraries. In this course we will use .NET. (Another popular implementation is Mono.)

To get started writing C# programs in Visual Studio Code, first install the .NET 5.0 SDK. Then, to begin a new project:

  1. Create an empty directory to hold your project.

  2. In Visual Studio Code, choose File → Open Folder… and choose the directory you created in step 2.

  3. Press Ctrl + ` to open a terminal window inside Visual Studio Code.

  4. In the terminal window, type

      $ dotnet new console

    A new C# project will be created, including a file Program.cs that you will see in the sidebar on the left. Double-click this file to open it.

  5. You may see a message "The 'C#' extension is recommended for this file type". If so, click the Install button under the message. Visual Studio Code will install the extension that provides C# support.

  6. You will see a message "Required assets to build and debug are missing. Add them?". Click the Yes button under the message.

To build and run your program, first open a terminal either inside Visual Studio Code, or in an external window. To get to a terminal inside Visual Studio Code, choose View → Terminal or press Control + `. Alternatively, to open an external terminal window, click anywhere in your source file or the sidebar, then press Ctrl+Shift+C.

To build your program, type

$ dotnet build

To run it, type

$ dotnet run

Speeding things up

Unfortunately the 'dotnet build' and 'dotnet run' commands are a bit sluggish, and also produce more output that is necessary. You can make 'dotnet build' quieter and a tad faster by specifying some extra arguments:

$ dotnet build --no-restore --nologo --verbosity q

You can run your program more quickly by running it directly instead of via the 'dotnet run' command. Assuming that you are using .NET 5.0, the generated executable will typically be in the bin/Debug/net5.0 directory. So if your project name is "game", you can run

$ bin/Debug/net5.0/game

This will start instantly, unlike "dotnet run".

On my Linux machine, I've set up aliases 'cb' and 'cr' for building and running C# programs like this:

alias cb='dotnet build --no-restore --nologo --verbosity q'
alias cr='proj=(*.csproj); bin/Debug/net*/"${proj%%.*}"'

The 'cr' command above runs the executable in the bin/Debug/net5.0 directory that has the same name as the .csproj file in the current directory.

Debugging C# code in Visual Studio Code

The Visual Studio Code debugger works well with C#. However, by default you can't enter any input during a debugging session, which is an unfortunate limitation. To fix this, open the project's launch.json file, which you can find in the explorer sidebar on the left. Then change the "console" property from "internalConsole" to either "integratedTerminal" if you want the debugger input/output to appear inside the terminal pane in Visual Studio Code, or "externalTerminal" if you want to use an external terminal window.

Before running the debugger, you probably want to set a breakpoint at the beginning of your Main() method so that the debugger will stop there. To do that, click on the first line in Main() and press F9 to create a breakpoint. Now type F5 to launch the debugger. The debugger output will appear in the Debug Console pane, but you will need to switch to the Terminal pane to see your program's output and enter input (assuming that you changed the "console" property as suggested in the previous paragraph).

For more information about building, running and debugging, see the page Get started with C# and Visual Studio Code at Microsoft's site.