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#
Nullable Reference Types
MirrorLesson 5 of 10
Lesson 5

Nullable Reference Types

Null Safety

Introduction

In this lesson, you'll learn about nullable reference types 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 null safety.

C#
In C#:

C# has its own approach to null safety, 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# 8+ nullable reference types (#nullable enable)
#nullable enable

int GetLength(string? s) {
    return s?.Length ?? 0;
}

class Config {
    public string Host { get; set; } = "";
    public int? Port { get; set; } // nullable int
}

void Connect(Config config) {
    int port = config.Port ?? 3000;
}

Comparing to TypeScript

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

TS
TypeScript (What you know)
// TypeScript strict mode — null safety enforced by compiler
function getLength(s: string | null): number {
  return s?.length ?? 0;
}

interface Config {
  host: string;
  port?: number; // optional = string | undefined
}

function connect(config: Config) {
  const port = config.port ?? 3000;
}
Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

C# nullable reference types require #nullable enable (or project-wide setting)

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

string? in C# means nullable, exactly like string | null in TypeScript strict mode

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

?. optional chaining and ?? nullish coalescing work identically in C#

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

C#
In C#:

Value types (int, bool) use int? (Nullable<int>) to allow null

Step-by-Step Breakdown

1. Enabling Nullable Context

C# nullable reference types are opt-in per file (#nullable enable) or project-wide (via .csproj). Without it, all references can be null silently.

TS
TypeScript
// TypeScript: enable via tsconfig { "strict": true }
C#
C#
// Per file:
#nullable enable

// Or in .csproj:
<Nullable>enable</Nullable>
Rule of Thumb
Always enable nullable context project-wide in new C# projects. It's the equivalent of TypeScript's strict mode.

2. string? — Nullable Reference

In nullable context, string is non-nullable (must always have a value) and string? is nullable. This mirrors TypeScript's string vs string | null.

TS
TypeScript
let name: string = "Alice"; // non-nullable
let maybe: string | null = null; // nullable
C#
C#
string name = "Alice"; // non-nullable
string? maybe = null; // nullable

3. ?. and ?? Work Identically

Optional chaining (?.) and nullish coalescing (??) have identical syntax and semantics in C# and TypeScript. Anders designed both.

TS
TypeScript
const city = user?.address?.city ?? "Unknown";
C#
C#
string city = user?.Address?.City ?? "Unknown";

4. Nullable Value Types: int?

TypeScript numbers are always reference-like. C# distinguishes value types (int, bool, double) from reference types. Use int? (Nullable<int>) to allow null for value types.

TS
TypeScript
let count: number | null = null;
C#
C#
int? count = null;
if (count.HasValue) {
    Console.WriteLine(count.Value);
}
// or:
int actual = count ?? 0;

Common Mistakes

When coming from TypeScript, developers often make these mistakes:

  • C# nullable reference types require #nullable enable (or project-wide setting)
  • string? in C# means nullable, exactly like string | null in TypeScript strict mode
  • ?. optional chaining and ?? nullish coalescing work identically in C#
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

  • Enable #nullable in C# — equivalent to strict: true in tsconfig
  • string? in C# nullable context maps exactly to string | null in TypeScript
  • ?. and ?? work identically in both C# and TypeScript
  • Value types (int, double) need int? syntax for nullable — reference types just use T?
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your TypeScript code in C# to practice these concepts.
PreviousNext