Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

nullable-reference-types

Nullable reference types

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Nullable reference types: the syntax, patterns, and safety habits you need before advancing in C#.

Nullable reference types prevent null bugs at compile time—teams enable them in new projects.

You will apply Nullable reference types in contexts like: .NET services, Unity games, and Windows-centric tooling.

Write C# with Console.WriteLine (top-level or Program), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

C# 8+ nullable reference types (NRT) add compile-time warnings for possibly-null strings and objects—closing a gap where reference types could always be null at runtime like in Java. Value types already had int? syntax.

Annotations

#nullable enable
string name = GetName();      // warning if GetName may return null
string? maybe = GetName();    // explicitly nullable
if (maybe is not null) {
    Console.WriteLine(maybe.Length);
}

The compiler tracks flow state after null checks—pattern matching integrates cleanly.

Null-forgiving operator

string sure = GetName()!;  // suppress warning—use sparingly

Prefer guard clauses and validation over sprinkling !.

Important interview questions and answers

  1. Q: string vs string?
    A: string? tells the compiler null is allowed; plain string is non-null by contract when NRT is enabled.
  2. Q: Do NRTs prevent NullReferenceException?
    A: No—they are warnings/analysis aids; runtime nulls still throw unless you check.

Self-check

  1. What suffix marks a nullable reference type?
  2. When is the ! operator appropriate?

Tip: Enable #nullable enable in new files—string? documents possible null; plain string is non-null by contract.

Interview prep

string vs string?

string? tells the compiler null is allowed; plain string is non-null by contract when NRT is enabled.

Do NRTs prevent NullReferenceException?

No—they are compile-time warnings; runtime nulls still throw unless you check.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ! null-forgiving?
  • Enable in legacy?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump