C# Foundations·C# from first file to first API

Types and nullability

C# is statically typed, and nullable reference types make absence explicit.

Beginner 12 min read Lesson 6 of 300 Challenge included
What you will be able to do
  • Use var only when the type is obvious from the right-hand side.
  • Prefer string? and explicit null checks over hopeful assumptions.
  • Use value types for small data and classes for identity.

In plain English

A variable type tells the compiler what operations are valid. With nullable reference types enabled, string means a real string and string? means it may be null. Fixing warnings is cheaper than chasing NullReferenceException later.

Program.csC#
string title = "C# Foundations";
int minutes = 12;
decimal price = 19.99m;
bool published = true;

string? subtitle = null;
Console.WriteLine(subtitle?.Length ?? 0);
Console.WriteLine(title + " · " + minutes + " min");
Your turn

Format a nullable name

Easy

Write a method that returns a display name or "Learner" when the input is null or whitespace.

CSHARP · starter
string Display(string? name)
{
    return name;
}
Check your understanding before moving on.
Knowledge checkQuestion 1

What does string? communicate in a nullable-enabled project?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 6 of 300 · 0% of course