SkillPilotAI

What are C# and .NET

C# is the language and .NET is the platform (runtime, libraries, tools) that runs it. Knowing how your code goes from C# to IL to machine code explains why one build runs on Windows, Linux and macOS, and it is a classic first interview question.

1.1 The puzzle: what is this .dll?

  • A small program adds 18% GST to a price of 500 and prints the total.
  • dotnet build produces Hello.dll, and running it prints Total: 590.
  • But the CPU does not understand C#, and the .dll is not machine code either: something translates in between.
C#
int price = 500;int total = AddGst(price); Console.WriteLine($"Total: {total}"); static int AddGst(int amount)    => amount * 118 / 100;
Output
$ dotnet build  Hello -> ...\Hello\bin\Debug\net10.0\Hello.dllBuild succeeded.    0 Warning(s)    0 Error(s) $ dotnet bin/Debug/net10.0/Hello.dllTotal: 590

1.2 Why not machine code directly?

  • Every CPU understands only its own machine code: x64 (laptops) and ARM (phones, Macs) use different instructions.
  • Windows, Linux and macOS also have different rules.
  • Compiling straight to machine code would need a separate build for every CPU + OS combination.
  • So .NET works in two steps: compile to IL once, then JIT to machine code on each machine.

1.3 Step 1: compiler -> IL

  • The C# compiler (Roslyn) turns your code into IL (Intermediate Language), not machine code.
  • IL is CPU-independent; the .dll (an assembly) also carries metadata describing its types and methods.
  • Viewed with ILSpy, AddGst is: load amount, load 118, multiply, load 100, divide, return.
Output
$ ilspycmd -il bin/Debug/net10.0/Hello.dll.method assembly hidebysig static  int32 '<<Main>$>g__AddGst|0_0' (    int32 amount) cil managed  IL_0000: ldarg.0  IL_0001: ldc.i4.s 118  IL_0003: mul  IL_0004: ldc.i4.s 100  IL_0006: div  IL_0007: ret

1.4 Step 2: CLR + JIT -> machine code

  • At run time the CLR (Common Language Runtime) loads the .dll.
  • The first time AddGst is called, the JIT (Just-In-Time) compiler converts its IL into machine code for the current CPU.
  • DOTNET_JitDisasm prints the generated x64 code: imul multiplies, idiv divides.
  • Tiered compilation: the JIT first emits quick, simple code (Tier0); methods that run often are recompiled later with optimizations.
Output
$ DOTNET_JitDisasm='*AddGst*' \    dotnet bin/Release/net10.0/Hello.dll; Assembly listing for method ...AddGst|0_0(int):int (Tier0); Emitting BLENDED_CODE for generic X64 + VEX on Windows       push     rbp       mov      rbp, rsp       mov      dword ptr [rbp+0x10], ecx       imul     eax, dword ptr [rbp+0x10], 118       mov      ecx, 100       cdq       idiv     edx:eax, ecx       pop      rbp       ret; Total bytes of code 21Total: 590
From C# to machine code
  1. 1.C# code (Program.cs)

  2. 2.Roslyn compiler: IL + metadata -> Hello.dll

  3. 3.dotnet Hello.dll: the CLR loads the assembly

  4. 4.JIT: IL -> x64 / ARM machine code on first call

  5. 5.CPU runs it; hot methods are re-JITted with optimizations (tiered)

Compile once, JIT on every machine

1.5 The CLR's job: more than JIT

  • JIT: IL -> machine code.
  • GC (garbage collector): frees unused memory automatically.
  • Type safety checks and exception handling.
  • Code running under the CLR is therefore called managed code.
  • BCL (Base Class Library): ready-made features for files, collections, JSON and more.

1.6 Checkpoint: will it run on Linux?

  • Question: the same Hello.dll built on Windows, run on Linux without rebuilding: does it work?
  • Yes: the .dll holds IL, and the .NET runtime's JIT on Linux compiles it for that machine.
  • Run in a Docker Linux container: OS shows Ubuntu, the total is still 590.
C#
using System.Runtime.InteropServices; Console.WriteLine($"Total: {AddGst(500)}"); var os = RuntimeInformation.OSDescription;var cpu = RuntimeInformation.ProcessArchitecture;var version = Environment.Version; Console.WriteLine($"OS: {os}");Console.WriteLine($"CPU: {cpu}");Console.WriteLine($".NET: {version}"); static int AddGst(int amount)    => amount * 118 / 100;
Output
$ dotnet bin/Debug/net10.0/Hello.dllTotal: 590OS: Microsoft Windows 10.0.26200CPU: X64.NET: 10.0.14 $ docker run --rm \    -v ...\bin\Debug\net10.0:/app \    mcr.microsoft.com/dotnet/runtime:10.0 \    dotnet /app/Hello.dllTotal: 590OS: Ubuntu 24.04.5 LTSCPU: X64.NET: 10.0.14

1.7 Versions: target .NET 10 LTS

  • Old tutorials target net8.0, but support for .NET 8 and .NET 9 ends on 10 Nov 2026.
  • On a machine with only .NET 10 installed, a net8.0 app does not even run.
  • Even-numbered versions are LTS (3 years of support); odd-numbered are STS (2 years).
  • This course uses .NET 10 (LTS) and C# 14.
XML
<Project Sdk="Microsoft.NET.Sdk">  <PropertyGroup>    <OutputType>Exe</OutputType>    <ImplicitUsings>enable</ImplicitUsings>    <TargetFramework>net10.0</TargetFramework>  </PropertyGroup></Project>

1.8 Comparison: .NET Framework vs .NET 10

Point.NET Framework.NET (5+, now .NET 10)
PlatformsWindows onlyWindows, Linux, macOS
StatusMaintenance mode onlyActively developed, yearly releases
SourceClosed (mostly)Open source
Use for new projectsNoYes (.NET 10 LTS)

1.9 Common misconceptions

  • "C# is only for Windows": that came from the old .NET Framework; modern .NET is cross-platform.
  • "C# and .NET are the same": C# is a language; .NET is the platform with the runtime, libraries and tools.
  • "The .dll is machine code": it is IL + metadata; machine code is produced by the JIT at run time.

1.10 What can you build with C#?

AreaTechnology
Web APIs + websites (focus of this course)ASP.NET Core
Cloud services + background workers.NET worker services
Windows desktop appsWPF, WinForms
Mobile apps.NET MAUI
GamesUnity
  1. Course roadmap: C# basics + OOP.
  2. Advanced C#, LINQ, async.
  3. ASP.NET Core + Web API.
  4. EF Core + security.
  5. Testing, deployment and a capstone project (HTTP, JSON, SQL concepts live in the Web Basics track).

1.11 Interview tips

  • "How does code run in .NET?": the compiler turns C# into an assembly with IL + metadata; at run time the CLR loads it and the JIT converts IL to machine code for the current CPU.
  • The CLR also provides GC, type safety and exception handling: that is why it is called managed code.
  • Because the assembly holds IL, the same build runs on Windows, Linux and macOS.
  • Mention tiered compilation: quick Tier0 code first, optimized code for hot methods later.
  • Use .NET 10 LTS for new projects; even = LTS (3 years), odd = STS (2 years).