Install .NET and your first program
C# is a modern, typed and very versatile language: it is used for console applications, websites, Unity games, desktop tools and cloud services. In this course we use C# 13 on .NET 9 and we always work from the command line, because that way you see exactly what happens at each step and you do not depend on any graphical wizard.
What to install
You need two things. The first is the .NET SDK, which contains the compiler, the base libraries and the command line tool called dotnet. Download it from the official Microsoft page (search for "download .NET SDK") and pick version 9 or newer. The second is an editor: Visual Studio Code, which is free and light, with the "C# Dev Kit" extension (or just the "C#" extension). Any other editor works, but with VS Code the screens and shortcuts in this course will feel familiar.
When the installation finishes, open a new terminal (it must be a new one so that it loads the updated PATH) and check that everything works.
dotnet --version
dotnet --list-sdksIf you see a version number such as 9.0.100 (or higher), you are ready. If you get "dotnet is not recognized as a command", close all terminals and open a new one; if it persists, reinstall the SDK.
Create the project
A C# program lives inside a project: a folder with a .csproj file that describes how to build it. You do not write it by hand; the CLI generates it. Create a working folder, enter it and ask for a console application.
dotnet new console -n HolaMundo
cd HolaMundo
dotnet runThe new command creates the HolaMundo folder with two important files: HolaMundo.csproj and Program.cs. The run command builds the project and executes it; you will see the message Hello, World! in the terminal. You have just run your first C# program.
Anatomy of Program.cs
Open Program.cs in VS Code (code . from the folder). Since C# 9 there are top-level statements: the file can contain the statements directly, without writing a Program class or a Main method. The compiler adds them for you. Replace the content with this:
// My first C# program
Console.WriteLine("Hello from C#!");
Console.WriteLine("2 + 3 = " + (2 + 3));
Console.WriteLine("The year is " + DateTime.Now.Year);- Console is a .NET class; WriteLine writes a line and moves to the next one.
- Every statement ends with a semicolon (;).
- Text goes between double quotes; comments start with //.
- You do not need to write using System; because new projects enable "implicit usings".
Typical mistakes
Forgetting the semicolon produces error CS1002 (; expected). Writing console in lowercase produces CS0103, because C# is case sensitive: Console and console are different names. When the compiler fails, read the whole message: it tells you the file, the line and the column. A compile error is your friend; it warns you before running.
// Run these in the terminal, one per line:
// dotnet build -> compiles only
// dotnet run -> compiles and runs
// dotnet clean -> removes build output✎ 我的笔记
仅保存在此浏览器中。登录后可保存到账号。0/5000 字符