TS
C#

TypeScript to C#

10 lessons

Progress0%
1Introduction: Two Languages, One Mind2Type Systems: Structural vs Nominal3Classes: Advanced Features4Generics and LINQ5Nullable Reference Types6Async/Await7Decorators to Attributes8Ecosystem9File I/O10Records and Pattern Matching
All Mirror Courses
TS
C#
Introduction: Two Languages, One Mind
MirrorLesson 1 of 10
Lesson 1

Introduction: Two Languages, One Mind

Introduction

Introduction

In this lesson, you'll learn about introduction: two languages, one mind in C#. Coming from TypeScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
TS
From TypeScript:

In TypeScript, you're familiar with introduction.

C#
In C#:

C# has its own approach to introduction, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
// C# — also by Anders Hejlsberg (2000)
string name = "World";

string Greet(string who) => $"Hello, {who}!";

Console.WriteLine(Greet(name));

// Top-level statements available since C# 9
// (no class/Main boilerplate needed in modern C#)

record Point(double X, double Y);

Comparing to TypeScript

Here's how you might have written similar code in TypeScript:

TS
TypeScript (What you know)
// TypeScript — by Anders Hejlsberg (2012)
const name: string = "World";

function greet(who: string): string {
  return `Hello, ${who}!`;
}

console.log(greet(name));

interface Point {
  x: number;
  y: number;
}
Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

Both languages were designed by Anders Hejlsberg at Microsoft

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

C# uses $ for string interpolation instead of backticks

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

Modern C# (9+) supports top-level statements — no class wrapper needed

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

C# record types provide concise immutable data classes

Step-by-Step Breakdown

1. The Anders Connection

Anders Hejlsberg designed both Turbo Pascal, C#, and TypeScript. That's why the syntax feels so familiar. Many TypeScript features (optional chaining, null coalescing, generics) came from or were influenced by C#.

TS
TypeScript
const msg: string = `Hello, ${name}!`;
C#
C#
string msg = $"Hello, {name}!";

2. String Interpolation

TypeScript uses backtick template literals. C# uses $"..." interpolated strings. The expressions inside braces work identically.

TS
TypeScript
const greeting = `Hello, ${user.name}! You are ${user.age} years old.`;
C#
C#
string greeting = $"Hello, {user.Name}! You are {user.Age} years old.";

3. Top-Level Statements (C# 9+)

Modern C# no longer requires wrapping everything in a class with a Main method. You can write code at the top level, similar to a TypeScript module.

TS
TypeScript
// TypeScript: top-level code runs directly
const x = 42;
console.log(x);
C#
C#
// C# 9+ top-level statements:
var x = 42;
Console.WriteLine(x);
// No class wrapper needed
Rule of Thumb
Use top-level statements for scripts and small programs. Use classes for libraries and larger applications.

4. var vs let/const

C# var is type-inferred but still statically typed — unlike JavaScript's var. The compiler infers the type and enforces it. Use it like const in TypeScript.

TS
TypeScript
const count = 42; // TypeScript infers: number
C#
C#
var count = 42; // C# infers: int — still statically typed
Common Pitfall
C# var is not dynamic. The type is fixed at declaration — you cannot reassign a different type later.

Common Mistakes

When coming from TypeScript, developers often make these mistakes:

  • Both languages were designed by Anders Hejlsberg at Microsoft
  • C# uses $ for string interpolation instead of backticks
  • Modern C# (9+) supports top-level statements — no class wrapper needed
Common Pitfall
Don't assume C# works exactly like TypeScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Both TypeScript and C# were designed by Anders Hejlsberg — syntax similarities are intentional
  • C# uses $"..." interpolated strings instead of backtick template literals
  • C# 9+ top-level statements remove the class/Main boilerplate
  • C# var is type-inferred but fully statically typed
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your TypeScript code in C# to practice these concepts.
OverviewNext