Tools for writing and compiling C# code

To write and compile C# code, you have two choices. You can use a text editor along with a command-line compiler. Of you can use a full-featured IDE that has more powerful support for C# projects, including an integrated debugger.

An editor with a command-line compiler

If you are looking for a text editor for writing programs in any language, I recommend Geany on any platform (Linux, macOS, or Windows). Geany is a programmer’s text editor that serves as a lightweight IDE. It can perform syntax highlighting in C# and it’s easy to configure it to build C# programs from the command line.

Of course, alternatively you can use any other text editor that you like, such as Sublime or the classic editors Emacs or Vim.

You will also need a command-line C# compiler. Several different implementations exist:

On Linux or macOS, I recommend installing Mono. On Windows, I recommend installing the .NET Framework by installing Visual Studio, even if you only want to use a text editor. (Don’t install the .NET Framework Developer Pack, which includes an older version of the C# compiler). For details, read on.

Configuring Geany or another editor on Linux or macOS

First, install Mono. You want a recent version, i.e. 5.18.0 (or 5.16.0). On Linux, it is unlikely that your distribution includes a version of Mono that is this new, so you should probably add the Mono repository to your system to get the package from there.

Once you have a text editor and a C# compiler, configure your editor to build and run C# programs if possible. Mono comes with two C# compilers: mcs (the classic Mono C# compiler) and csc (a newer C# compiler from Microsoft). You should definitely use csc to build, since only it has full support for C# 7, the latest version.

To configure Geany to use csc:

  1. Open any C# source file in Geany; this will be a file whose name ends in .cs. (If you don’t have any such file, simply create an empty document and save it as test.cs.)

  2. With the C# source file open, choose the menu item Build → Set Build Commands.

  3. A dialog will open. In the section “C# commands”, you’ll see a command beginning with “mcs”. Replace this entire command with

      csc "%f"
  4. Press OK to close the dialog.

Now you can build any C# program by pressing the Compile toolbar button, or by pressing F8. To run the program, press the Run toolbar button, or press F5.

If you are using another editor, you can configure it similarly.

Note that the csc compiler generates an executable file whose name ends in .exe. On Linux or macOS, you cannot execute this file directly from the command line. You need to run it with mono:

$ csc hello.cs
$ mono hello.exe
hello, world
$

Geany comes preconfigured to run C# executables in this way.

Configuring Geany or another editor on Windows

First install Visual Studio (see the instructions in the section “Installing Visual Studio Community” below). Even if you are planning to use another editor, this is the easiest way to get an up-to-date version of the .NET Framework and the command-line compiler.

Unfortunately the Visual Studio installer does not put the csc compiler into your PATH. So you need to figure out where csc is, and then configure your editor to find it there. Here is how to configure Geany:

  1. In the start menu, search for the word “command”. This should bring up a menu item “Developer Command Prompt for VS 2017”. Click that.

  2. In the command prompt window, type

    where csc

    This will produce output like this:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Command\MSBuild\15.0\Bin\Roslyn\csc.exe
    
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

    (The exact paths might differ on your system.)

  3. In the command prompt window, use the mouse to select the first of the two paths above (the one containing “Microsoft Visual Studio”). Leave it selected.

  4. Start Geany. Open any C# source file in Geany; this will be a file whose name ends in .cs. (If you don’t have any such file, simply create an empty document and save it as test.cs.)

  5. With the C# source file open, choose the menu item Build → Set Build Commands.

  6. A dialog will open. In the section “C# commands”, you’ll see a command beginning with “mcs”. Delete this entire command.

  7. Right click in the text box where you deleted the mcs command, and choose Paste. This will paste in the path that you selected in step 3.

  8. Add double quotes (") at the beginning and end of the path. Now add "%f" (including the quotes) at the end. The command should now look like this:

    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Command\MSBuild\15.0\Bin\Roslyn\csc.exe" "%f"

    (Again, the exact path might differ on your system.)

  9. Now look at the text in the “Execute commands” section further down in the window. You’ll see a command beginning with “mono”. Delete the word “mono”. The command should now look like this:

    "%e.exe"
  10. Close the dialog. You should now be able to use Geany to compile and run C# programs.

If you are using another editor, adapt the steps above.

A full-featured IDE

If you want to use a full IDE, again you have a couple of choices:

These programs have similar user interfaces.

On Linux, MonoDevelop is your only choice from among these two. On macOS or Windows, I recommend Visual Studio. You can download the Community edition for free. See below for details.

Installing MonoDevelop

You probably want the latest version of MonoDevelop (7.6) since it supports C# 7. To install it on Linux, follow the installation steps on the MonoDevelop page, adding the Mono repository to your system.

Getting started with MonoDevelop

  1. Start MonoDevelop.

  2. In the Solutions area at the left, click New…

  3. A window will appear entitled “Choose a template for your new project”. In the left column, select .NET. Now make sure that Console Project is highlighted, and click Next.

  4. You will see a window “Configure your new Console Project”. Enter a project name such as “hello”. You may change the directory in the Location field if you like. Now click Create.

  5. In the main window, you will now see a “hello, world” C# program. Edit it as you like. To build your program, choose Build → Build All or press F8. To run your program, choose Run → Restart Without Debugging or press Ctrl+F5.

Installing Visual Studio Community

  1. Download Visual Studio Community 2017 from the Visual Studio home page.

  2. Run the installer.

  3. You will see a window offering various install options. On the left under Workloads / Windows you will see three boxes:

    Check the middle box, i.e. “.NET desktop development”.

  4. Click Install in the lower right corner of the window.

Getting started with Visual Studio Community

  1. Start Visual Studio.

  2. Click the New Project… button in the center of the window.

  3. A window will appear entitled “Choose a template for your new project”. In the left column, select .NET under Other. Now make sure that Console Project is highlighted, and click Next.

  4. You will see a window “Configure your new Console Project”. Enter a project name such as “hello”. You may change the directory in the Location field if you like. Now click Create.

  5. In the main window, you will now see a “hello, world” C# program. Edit it as you like. To build your program, choose Build → Build All. To run your program, choose Run → Restart Without Debugging.