To write and compile C# code, you need two things:
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).
An implementation of the C# compiler and runtime libraries. In this course we will use .NET.
To get started writing C# programs in Visual Studio Code, first install the .NET 6.0 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.
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
You can run your program more quickly by running it directly instead of via the 'dotnet run' command. Assuming that you are using .NET 6.0, the generated executable will typically be in the bin/Debug/net6.0 directory. So if your project name is "game", you can run
$ bin/Debug/net6.0/game
This will start instantly, unlike "dotnet run".
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.