Getting Started

Course 1 · Ch 1
Getting Started
Same two-stage compilation model as java1-1's JVM — C# was built as Microsoft's direct answer to Java

This course sits deliberately next to Java throughout — not because the two are interchangeable, but because they share more architecture than most language pairs on this site, and diverge in specific, namable places. This chapter starts with what they share.

The CLR vs. the JVM

C# compiles to IL (Intermediate Language) — not directly to machine code — and the CLR (Common Language Runtime) JIT-compiles that IL at runtime, exactly the same two-stage model java1-1 described for the JVM and Java bytecode. This is a genuine point of similarity worth naming explicitly, not a coincidence — C# inherited this architecture on purpose, the same "write once, run wherever the runtime exists" tradeoff Java made first.

The dotnet CLI

dotnet new console -o MyApp # scaffolds a new project dotnet run # compiles AND runs in one command dotnet build # compiles only, without running

Where java1-1 used two separate tools — javac to compile, java to run — dotnet is one unified CLI covering project scaffolding, building, and running. dotnet run alone does what javac + java together did.

Top-Level Statements

// Program.cs — the entire program, no class or Main required Console.WriteLine("Hello, C#!");

Since C# 9, a file can skip the class-and-Main boilerplate entirely for the simplest programs — a real, direct contrast to java1-1's strict "everything lives in a class, with an explicit public static void Main" requirement. Under the hood, though, the compiler still generates that exact class-and-Main shape automatically — this is real syntactic sugar over the same model, not a fundamentally different one.

What's Really There

class Program { static void Main(string[] args) { Console.WriteLine("Hello, C#!"); // what the top-level version compiles down to } }

This is the explicit, traditional form — the same shape java1-1 required from the very first line. Larger, real applications generally still write Main explicitly; top-level statements are mainly a convenience for small programs, scripts, and quick experiments.

C#'s Origin as Microsoft's Direct Answer to Java

C# was created in 2000, led by Anders Hejlsberg — previously the architect behind Turbo Pascal and Delphi, later the designer of TypeScript. Its creation followed directly from a Sun Microsystems lawsuit over Microsoft's own non-compliant Java implementation, J++ — rather than continue fighting over Java itself, Microsoft built its own language and runtime from scratch. This is real, documented history, not just a marketing framing — and it's exactly why C# and Java's architectures line up as closely as they do.

AspectJava (java1-1)C#
RuntimeJVMCLR
Intermediate formatbytecodeIL
Compile & run toolingjavac, then java — two toolsdotnet — one unified CLI
Minimum program shapealways an explicit class + maintop-level statements allowed (C# 9+)
Top-level statements suit scripts, not necessarily large apps
Reach for top-level statements for small programs, quick experiments, and this course's own early examples — real multi-file applications generally still write Main explicitly once there's more than one entry point's worth of setup to reason about.
Only one file per project may use top-level statements
A project can have exactly one file containing top-level statements — attempting a second produces a real compile error, since the compiler can only generate one implicit Main per project.

Coding Challenges

Challenge 1

Create a new console project with dotnet new console, write a top-level-statement Program.cs that prints two lines of your choosing, and run it with dotnet run.

📄 View solution
Challenge 2

Rewrite Challenge 1's program using the explicit class Program with a static void Main(string[] args) method instead of top-level statements, and confirm it produces identical output.

📄 View solution
Challenge 3

Explain in a comment why C# and Java both use a two-stage compile-then-JIT model rather than compiling directly to native machine code, tying your answer to C#'s own origin as a direct answer to Java.

📄 View solution

Chapter 1 Quick Reference

  • The CLR JIT-compiles IL at runtime — the same two-stage model as the JVM and Java bytecode (java1-1)
  • dotnet is one unified CLI for scaffolding, building, and running — Java needed javac and java separately
  • Top-level statements (C# 9+) skip the class/Main boilerplate for simple programs — real sugar, not a different model underneath
  • Only one file per project may use top-level statements
  • C# was created in 2000 as Microsoft's direct answer to Java, led by Anders Hejlsberg — later also the designer of TypeScript
  • Next chapter: variables and basic types — value types vs. reference types, and var type inference