Your First C# Program: Top-Level Statements, the Compiler and .NET
Write and run a first C# program with dotnet run, understand what the compiler and the .NET runtime do, and check the real build output for the target framework and the assembly file.
In this lesson you will
- Write and run a C# program
- Explain the compiler and the .NET runtime
- Read top-level statements
- Inspect real build output
About 28 minutes
Omar wants to count his cups
Omar runs a small coffee cart and tracks his orders in a notebook. He wants a program that shows his numbers for the day, and he has heard that C# is a good choice because it works for web services, desktop programs and games. C# is a modern, typed, object-oriented language made by Microsoft and used with .NET, a free, open source platform that runs on Windows, macOS and Linux. This lesson runs a tiny program and then opens up the files that the tools produced, so that nothing is magic.
From source to running program
You write source code in a file that ends in .cs. The C# compiler turns it into an assembly, a .dll file that holds a portable intermediate language called IL together with metadata. The .NET runtime loads the assembly and compiles the IL to machine code for your processor while the program runs, which is called just-in-time compilation. The command dotnet run does all of it: it builds the project and starts the result. A project is described by a small .csproj file that names the target framework, such as net8.0, and the language settings.
| Step | Tool | Result |
|---|---|---|
| Write | Any editor | Program.cs |
| Build | dotnet build | app.dll and app.runtimeconfig.json |
| Run | dotnet run or dotnet app.dll | Output on screen |
Since C# 9 a simple program can use top-level statements: you write the statements directly in the file, without a class and a Main method around them, and the compiler adds those for you. Larger programs still use classes, as you will see from lesson 7 on. The first line below prints a greeting with Console.WriteLine, and $”…” is an interpolated string, where anything in curly braces is replaced by its value.
Console.WriteLine("Welcome to Omar's Cafe");
int cups = 12;
string cart = "Omar's Cart";
Console.WriteLine($"{cart} has {cups} cups ready");
Console.WriteLine($"Running on .NET {Environment.Version.Major}");
Console.WriteLine($"This assembly is called {System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}");
Welcome to Omar's Cafe
Omar's Cart has 12 cups ready
Running on .NET 8
This assembly is called app
The program printed that the cart has 12 cups ready, that it runs on .NET 8, and that the assembly is called app, which is the project name. Interpolation put the values of cups and cart into the text with no plus signs.
To see what the build produced, the course opened the output folder. The file app.runtimeconfig.json names the target framework as net8.0, which matches the major version the program reported. The assembly app.dll is 5,120 bytes, and its first two bytes are MZ, the signature of the Windows portable executable format that .NET uses for assemblies on every operating system. Running the same dll on Linux works because the runtime, not the dll, is specific to the system. This is the proof that the compiler made a real, versioned file and that the .NET runtime, which is a separate installed program, is what executes it.
Things that trip beginners
- C# is case sensitive: Console and console are different names.
- Every statement ends with a semicolon.
- Text goes in double quotes and single characters in single quotes.
- Curly braces mark blocks, and indentation is for people.
- Warnings are worth reading: they often point at real mistakes.
What the project file does
The .csproj file is small but important. It names the target framework, turns on features such as nullable reference types and implicit using directives, and lists the packages that the project depends on. When you run dotnet build, the SDK reads this file and decides what to compile and where to put the result. Editing it by hand is normal, and it is plain XML that you can read, which makes a .NET project easy to understand and to share: anyone with the same SDK can build it from the folder alone.
The runtime runs programs. To build them you need the .NET SDK. Download it from the official Microsoft site and check the version with dotnet –version.
One missing semicolon can cause several errors. Fix the first, build again, and see how many remain.
Every program in this lesson was built and run with the .NET 8 SDK and C# 12 while the course was written. .NET and C# release every year, so check the official Microsoft documentation for the current version and for features added after .NET 8.
Practice
Run dotnet new console in an empty folder, open Program.cs and change the greeting. Then run dotnet build and find the dll in the bin folder. Remove a semicolon on purpose and read the error message, which shows the file, the line and a code such as CS1002 that you can look up in the documentation.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does the C# compiler produce?
- A web page
- An assembly in intermediate language
- A text file
- A finished machine program for one CPU
The runtime compiles IL to machine code while running.
What does dotnet run do?
- Builds the project and runs it
- Only installs .NET
- Deletes files
- Opens an editor
It does both steps.
What is a top-level statement?
- A statement written without a surrounding class and Main method
- A very important line
- A comment
- A type
The compiler adds the class for you.
How did the course check the target framework?
- By guessing
- By its file colour
- It did not
- By reading app.runtimeconfig.json
It said net8.0.
Why does the same dll run on several systems?
- The dll is huge
- C# is interpreted line by line
- It needs no runtime
- The runtime is specific to the system, the IL is not
The runtime compiles IL to the local processor.
Summary
- C# is compiled to IL in an assembly, and the .NET runtime runs it.
- dotnet run builds and runs a project.
- Top-level statements let a program start without a class.
- The build output names the target framework and holds the dll.
Questions
Is C# only for Windows?
No. .NET runs on Windows, macOS and Linux. Check the official documentation for supported versions.
Do I need Visual Studio?
No. You can use any editor and the dotnet command line. Visual Studio and other editors add helpful tools.
What is NuGet?
The package manager for .NET, used to add libraries to a project. A later lesson mentions it.
