Lesson 1 of 24
In progress
Install .NET and run a program
Install the .NET SDK, create a console project, run it, and understand what dotnet build actually produces.
C# runs on .NET, and the .NET SDK is the only install you need. One command creates a project, and another runs it — no separate compiler invocation and no project file to write by hand.
What you will learn
- Tell the SDK from the runtime.
- Create, run and build a console project.
- Read the files
dotnet newproduces.
SDK and runtime
The runtime executes compiled programs. The SDK includes the runtime plus the compiler, the dotnet command and the templates used to create projects.
Install the current long-term-support SDK from the .NET download page or your package manager, then check:
dotnet --version
dotnet --list-sdks
The first prints the version in use; the second shows every installed SDK. C# and .NET version together: a project targets a framework such as net9.0, and the SDK must be at least that new.
The first project
dotnet new console -o Hello
cd Hello
dotnet run
Three commands: create a project in a folder, enter it, run it. dotnet run restores packages if needed, builds, and starts the program.
The generated Program.cs is one line:
Console.WriteLine("Hello, World!");
Since C# 9, a file can contain top-level statements, and the compiler wraps them in a generated Main method. That is why a complete program needs no class and no namespace. For a larger program, an explicit Main or an explicit class is still perfectly valid.
What dotnet new produced
Hello/
Hello.csproj
Program.cs
obj/
Hello.csproj is the project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
OutputType says this builds an executable. TargetFramework names the runtime version. ImplicitUsings adds common namespaces such as System automatically, which is why Console needs no using. Nullable turns on nullable reference type warnings — leave it on.
obj/ and bin/ are generated. Add them to .gitignore; commit the .csproj and .cs files.
Build, run, publish
dotnet build
dotnet run
dotnet publish -c Release
build compiles to bin/Debug/net9.0/. run does the same and starts the program. publish produces a deployable folder, optionally self-contained so the target machine needs no .NET install.
Debug and Release differ in optimisation and in which symbols are included. Use Release when you measure performance; Debug builds are deliberately easier to step through.
The file layout of a real project
Shop/
Shop.csproj
Program.cs
Models/Order.cs
Services/Checkout.cs
C# has no required folder-to-namespace mapping, but the convention is one folder per namespace, with the namespace matching the folder path. Following it keeps using statements predictable.
Adding a package
dotnet add package Humanizer
That edits the .csproj and restores the package. The reference lands in the project file, so a teammate who clones the repository gets the same dependency.
<ItemGroup>
<PackageReference Include="Humanizer" Version="2.14.1" />
</ItemGroup>
The version is explicit by default, which keeps builds reproducible. dotnet list package --outdated shows what can be updated, and updating is a decision rather than a background event.
Running a single file
Recent SDKs support file-based programs:
dotnet run app.cs
That is convenient for a scratch experiment and not a substitute for a project. Once there are two files or a package reference, create a project.
Common first errors
dotnet: command not found— the SDK is not on the PATH, or only the runtime is installed.The current .NET SDK does not support targeting .NET 9.0— the SDK is older than the target framework.Program does not contain a static 'Main' method— top-level statements are mixed with an explicitMain, or the file is not included in the project.- A package restores but the type is not found — the
usingis missing, and implicit usings do not include every namespace.
The version errors are the most common, and dotnet --info prints the SDK, runtime and target framework in one place.
Installing only the .NET runtime and then wondering why dotnet new is missing. The templates and compiler ship with the SDK, so install that instead.
Create a console project called Greeter, change the message to include today’s date, run it, then build in Release and find the output folder.
Show solution
Console.WriteLine($"Hello on {DateTime.Now:yyyy-MM-dd}"); dotnet run prints the message. dotnet build -c Release writes to bin/Release/net9.0/, where the .dll and its dependencies live. The executable next to it is a launcher that starts the runtime with that assembly, which is why the folder must be published as a whole.
Recap
- The SDK contains the compiler and templates; the runtime only executes programs.
dotnet new console,dotnet runanddotnet buildcover the daily loop.ImplicitUsingsandNullableare on by default in new projects; keep both.
Next, the first real program: output, input and the shape of a C# file.
Which .NET version should I install?
Can I use Visual Studio instead?
Progress is saved in this browser only. No account, no email.