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 one of these:
Visual Studio Code (cross-platform)
Visual Studio (Windows only)
Geany, a programmer's text editor (cross-platform)
Of course, many other editors exist as well (e.g. Sublime, Atom, MonoDevelop).
2. An implementation of the C# compiler and runtime libraries. Several possibilities exist:
.NET Core (cross-platform)
.NET Framework (Windows only)
Mono (cross-platform)
Combining the above, here are my recommendations:
On Linux or macOS, if you want an IDE, use Visual Studio Code with .NET Core. If you want a lightweight editor, use Geany (or another editor) with Mono.
On Windows, If you want an IDE, use either Visual Studio Code or Visual Studio, and .NET Core. If you want a lightweight editor, use Geany (or another editor) plus .NET Framework.
Below are detailed instructions for getting any of these recommended tool combinations to work:
Visual Studio Code is a popular open-source editor that runs on all major platforms, and supports many different programming languages.
To get started writing C# programs in Visual Studio Code, first install the .NET Core SDK. Then, to begin a new project:
Create an empty directory to hold your project.
In Visual Studio Code, choose File → Open Folder… and choose the directory you created in step 2.
Press Ctrl + `
to open a terminal window inside Visual Studio Code.
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.
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.
You will see a message "Required assets to build and debug are missing. Add them?". Click the Yes button under the message.
Choose the menu item Terminal → Configure Default Build Task… You will see a message "Select the task to be used as the default build task". Click "build".
You will need to perform the above steps only once for each project.
You will run your
program in a terminal either inside Visual Studio Code, or in an
external terminal 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.
After you make changes to your code and save it, there are several possible ways to run the program:
Type "dotnet run" in a terminal. This will build and run the program. This command works but is a bit sluggish – it takes a few seconds to build and run even a small "hello, world" application.
Build the program and then run the generated executable, which takes more work but is a tad faster. To build, choose Terminal → Run Build Task, or press Ctrl+Shift+B. You will see output such as the following:
> Executing task: dotnet build /home/adam/Desktop/prog/game.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary < Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 23.89 ms for /home/adam/Desktop/game/game.csproj. game -> /home/adam/Desktop/game/bin/Debug/netcoreapp3.1/game.dll Terminal will be reused by tasks, press any key to close it.
Click in the build output and press Enter to dismiss it.
If you are using .NET Core 3.1, the generated executable will typically be in the bin/Debug/netcoreapp3.1 directory. So if your project name is "game", you would run "./bin/Debug/netcoreapp3.1/game" from the terminal. This will start instantly, unlike "dotnet run". (The speed increase is especially significant if you want to run the program several times in succession.)
Run the command "dotnet watch run" in a terminal. This command will watch your source directory, waiting for any source file to change. After each change, it will rebuild and run the program automatically. This is convenient since you don't have to manually rerun the program each time it changes.
Run the command "Debug → Run Without Debugging". This works, but I don't generally recommend it since it is inconvenient in various ways: it always rebuilds the program even if it hasn't changed, and it places output in the Debug Console window, where it is relatively hard to read and is always preceded with an annoying legal message "You may only use the Microsoft .NET Debugger..."
With approaches (1) or (2) above, you can redirect the input from a file as you run, e.g.
$ dotnet run < sample.in
or
$ ./bin/Debug/netcoreapp3.1/game < sample.in
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.
First install Visual Studio Community:
Download Visual Studio Community 2019.
Run the installer.
You will see a window offering various install options. On the left in the Desktop & Mobile section you will see a box ".NET desktop development". Check this box.
Click Install in the lower right corner of the window.
To get started:
Start Visual Studio.
Select "Create a new project" at the right side of the window.
A window “Create a new project” will appear listing many possible project types. Select "Console App (.NET Core)". There may be two items with this name; be sure to select the one with a C# icon to its left, not the one with the VB (= Visual Basic) icon. Now click the Next button.
You will see a window “Configure your new project”. Enter a project name such as “hello”. You may change the directory in the Location field if you like. Now click Create.
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 Solution, or press Ctrl+Shift+B. To run your program, choose Debug → Restart Without Debugging, or press Ctrl+F5.
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.
First, install Mono.
You want a recent version, i.e. 6.8.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.
Then install the mono-devel
and
mono-roslyn
packages to get all the
necessary tools.
Once you have a text editor and Mono,
configure your editor to build and run C# programs. 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# 8, the latest
version.
To configure Geany to use csc
:
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
.)
With the C# source file open, choose the menu item Build → Set Build Commands.
A dialog will open. In the section “C# commands”, you’ll see a command beginning with “mcs”. Replace this entire command with
csc "%f"
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.
To redirect the input from a file as you run your program, type
$ mono hello.exe < sample.in
On Windows, I recommend installing the .NET Framework by installing Visual Studio, even if you only want to use a text editor. This is the easiest way to get an up-to-date version of the .NET Framework and the command-line compiler. (Don’t install the .NET Framework Developer Pack, which includes an older version of the C# compiler).
To install Visual Studio, see the instructions in the section Getting Started with Visual Studio below.
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:
In the start menu, search for the word “command”. This should bring up a menu item “Developer Command Prompt for VS 2019”. Click that.
In the command prompt window, type
where csc
This will produce output like this:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
(The exact paths might differ on your system.)
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.
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
.)
With the C# source file open, choose the menu item Build → Set Build Commands.
A dialog will open. In the section “C# commands”, you’ll see a command beginning with “mcs”. Delete this entire command.
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.
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\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe" "%f"
(Again, the exact path might differ on your system.)
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"
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.
To redirect the input from a file as you run your program, you will need to run it from the command line. Type
hello.exe < sample.in