What C# is and where it runs

What C# is

C# is a compiled, statically typed, object-oriented language. Each of those words changes the way a program is written, and the first program already shows two of them.

A C# file is a list of declarations and statements. The compiler checks the whole file before anything runs: a name it cannot resolve, a type that does not fit or a member that does not exist is an error, and no program is produced. That is what statically typed means here, and it is why the first lesson of every language course is the compiler.

The compiler is part of the .NET SDK. It turns the file into an assembly, a .dll file of intermediate language, and the runtime executes that. The output of a build is not a native binary unless you ask for one: the usual program is a small assembly plus the shared runtime that is already installed.

The compiler, the runtime and the GC

Three things happen between the source and the output of a program. The compiler (Roslyn, inside the SDK) reads the source and writes the assembly. The runtime (the CLR) loads that assembly, compiles each method to machine code the first time it is called, and runs it. The garbage collector reclaims the memory of objects nothing refers to any more, so a program does not free its objects by hand.

That is also why the runtime prints its own version: a program written for .NET 10 runs on the .NET 10 runtime of the machine, and the version of the language and the version of the runtime are two different numbers. The program below prints the runtime it found itself on.

using System.Runtime.InteropServices; Console.WriteLine("Hello, C#"); Console.WriteLine($"runtime: {Environment.Version}"); Console.WriteLine($"framework: {RuntimeInformation.FrameworkDescription}"); Console.WriteLine($"architecture: {RuntimeInformation.ProcessArchitecture}");

Save it as Program.cs in a project and run it:

dotnet run

Hello, C#
runtime: 10.0.15
framework: .NET 10.0.15
architecture: X64

The three lines after the greeting are the machine talking about itself, and they are what this container printed. A reader on another machine sees the runtime and the architecture of that machine, which is the point: one assembly, any supported system.

RuntimeInformation is in System.Runtime.InteropServices, and the project of this course imports the common namespaces for you: the lesson on namespaces and using shows what ImplicitUsings adds and how to turn it off.

One program, built and run

The greeting above is a top-level statements program: no class, no method, the statements are the entry point. It is the form the SDK writes for a new project, and the form most of this course uses for a short example. The same idea written as a class, with a value that carries a computation, is the program below.

class Temperature { private readonly double celsius; public Temperature(double celsius) { this.celsius = celsius; } public double Fahrenheit => celsius * 9 / 5 + 32; } class Program { static void Main() { var freezing = new Temperature(0); var boiling = new Temperature(100); Console.WriteLine($"water freezes at {freezing.Fahrenheit} F"); Console.WriteLine($"water boils at {boiling.Fahrenheit} F"); } }

dotnet run

water freezes at 32 F
water boils at 212 F

Two details are worth a pause. readonly on the field means the constructor is the only place allowed to write it, and => on Fahrenheit makes it a property computed on each read instead of a stored number. Both are checked by the compiler: a write to celsius in Main would not compile.

A class is a type

Temperature is a type the program defined, and new Temperature(0) is a value of that type. The compiler tracks it like any other: freezing.Fahrenheit is a double, and freezing.Celsius does not exist, so the compiler stops on it. That is the same check the runtime does for the type of a value at run time, which how to find out the type of a C# variable shows from the other side.

What you need

One package: the .NET SDK. It holds the compiler, the runtime, the project system and the dotnet command. The next lesson installs it and checks the version; the one after that creates a project, builds it and runs it.

dotnet --version

10.0.303

A new SDK arrives every November, and every second one is a long-term support release. The programs in this course were built with the version above, and the note at the end of each lesson names it.

Exercises

  1. Change the first program to print RuntimeInformation.OSDescription and RuntimeInformation.OSArchitecture. Which of the two tells you that the program runs in a container?
  2. Add a Kelvin property to Temperature. It needs a get body or an expression body; write it both ways and check that the output is the same.
  3. Make celsius writable outside the constructor and look at the error the compiler gives for the assignment in the constructor and outside it.
  4. Keep the class program but delete static from Main. Read the error before you change anything back.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does it mean that C# is statically typed?

    Answer

    The compiler checks the types of every expression before the program runs

    The compiler knows the type of every expression and refuses to build a program where one does not fit; the check happens before anything runs.

  2. Which three parts turn a C# source file into running output?

    Answer

    The compiler writes an assembly, the runtime compiles methods as they are called, the GC reclaims objects

    Roslyn writes the assembly, the CLR JIT-compiles methods on first call and runs them, and the garbage collector reclaims the objects nothing refers to.

  3. What does this program print? Console.WriteLine("Hello, C#"); Console.WriteLine($"runtime: {Environment.Version}");

    Answer

    Hello, C# / runtime: 10.0.15

    An interpolated string puts the value of Environment.Version on the line, and this container runs the 10.0.15 runtime; the two WriteLine calls make two lines.

  4. What does this program print? class Program { static void Main() { var t = new Temperature(100); Console.WriteLine(t.Fahrenheit); } } class Temperature { private readonly double celsius; public Temperature(double celsius) => this.celsius = celsius; public double Fahrenheit => celsius * 9 / 5 + 32; }

    Answer

    212

    Fahrenheit is computed from the stored 100 degrees Celsius, so the property returns 212.

  5. What does this program print? Console.WriteLine(1 + 2); Console.WriteLine("1" + 2);

    Answer

    3 / 12

    Two ints are added, while a string on the left of + makes the whole expression a string concatenation, so 1 and 2 are joined.

  6. A program writes var total = 0; and never prints anything. What happens when it is built and run?

    Answer

    It builds and prints nothing

    A C# program may do nothing at all; var is inferred from the initialiser, and nothing on the page prints the value.

  7. What is the difference between int and long in C#?

    Answer

    The number of bits they store: 32 against 64

    int is a 32-bit signed integer and long a 64-bit signed integer, which is why a long holds values an int cannot.

  8. A field is declared private readonly double celsius and the constructor assigns it. What does the compiler accept?

    Answer

    The assignment in the constructor only

    A readonly field may be assigned in its declaration or in a constructor of the same class, and nowhere else.

  9. Which command builds and runs a project in the current folder?

    Answer

    dotnet run

    dotnet run builds the project if needed and starts it; csc is not a command of the SDK, dotnet exec runs an assembly that was built already, and dotnet compile does not exist.

  10. What does this program print? Console.WriteLine($"{2 + 3} and {"2" + "3"}");

    Answer

    5 and 23

    The left expression adds two ints, the right one joins two strings, and the interpolated string holds both results.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev