beginner15 min

Introduction to C# and .NET

What .NET is, how C# compiles to IL and runs on the CLR, and the shape of a minimal C# program.

What you'll learn

  • Explain what .NET is (the runtime and base class library) and how C# relates to it
  • Describe the compile path from C# source to IL to native code run by the CLR
  • Identify the parts of a minimal C# program and what `dotnet run` does

Explanation

.NET is a free, cross-platform runtime and standard library (the "Base Class Library," or BCL) maintained by Microsoft. It's the platform C# code actually runs on -- much like the JVM is the platform Java runs on. .NET today runs identically on Windows, Linux, and macOS.

C# is one of several languages that target .NET (others include F# and Visual Basic .NET). When you compile a C# program, the compiler doesn't produce native machine code directly -- it produces Intermediate Language (IL), a CPU-independent bytecode. At run time, the Common Language Runtime (CLR) loads that IL and just-in-time (JIT) compiles it to native machine code for the machine it's actually running on. This is why the exact same compiled program can run unmodified on Windows, Linux, or macOS, as long as .NET is installed.

A minimal modern C# program can be as short as a few lines of top-level statements in a file conventionally named Program.cs -- no explicit class Program { static void Main() { ... } } boilerplate required (the compiler generates that scaffolding for you behind the scenes). Console.WriteLine(...) prints a line of text to standard output, and string interpolation ($"...{expression}...") lets you embed expressions directly inside a string literal.

You'll use the dotnet command-line tool constantly: dotnet run compiles and immediately runs a project in one step, convenient while developing (you'll see the full picture of the dotnet CLI in this course's final lesson).

Guided lab

Predict: A minimal C# program

C#Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

using System;

Console.WriteLine("Hello, .NET!");
int sum = 2 + 2;
Console.WriteLine($"2 + 2 = {sum}");

Stuck? Get a hint.

Common mistakes

  • Thinking C# compiles straight to native machine code the way C does -- it compiles to IL first, which the CLR JIT-compiles at run time.
  • Assuming .NET is a Windows-only technology -- modern .NET is fully cross-platform (Windows, Linux, macOS).
  • Expecting to need a `class Program { static void Main() }` wrapper in every file -- modern C# top-level statements let a file's code run directly without that boilerplate.

Knowledge check

Knowledge check

1. What does the C# compiler produce before the CLR JIT-compiles it to native code?
2. Which best describes modern .NET's platform support?
3. What does `dotnet run` do?

Takeaway

C# compiles to IL, which the CLR JIT-compiles to native code at run time -- this is what makes the same compiled .NET code run unmodified across Windows, Linux, and macOS.

Summary

.NET is the cross-platform runtime and library C# targets; C# source compiles to IL, the CLR JIT-compiles IL to native code, and `dotnet run` builds and runs a project in one step.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.