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.
In TypeScript, you're familiar with introduction.
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# — 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:
// 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;
}You may be used to different syntax or behavior.
Both languages were designed by Anders Hejlsberg at Microsoft
You may be used to different syntax or behavior.
C# uses $ for string interpolation instead of backticks
You may be used to different syntax or behavior.
Modern C# (9+) supports top-level statements — no class wrapper needed
You may be used to different syntax or behavior.
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#.
const msg: string = `Hello, ${name}!`;string msg = $"Hello, {name}!";2. String Interpolation
TypeScript uses backtick template literals. C# uses $"..." interpolated strings. The expressions inside braces work identically.
const greeting = `Hello, ${user.name}! You are ${user.age} years old.`;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.
// TypeScript: top-level code runs directly
const x = 42;
console.log(x);// C# 9+ top-level statements:
var x = 42;
Console.WriteLine(x);
// No class wrapper needed4. 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.
const count = 42; // TypeScript infers: numbervar count = 42; // C# infers: int — still statically typedCommon 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
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