Nullable reference types (awareness) & annotations (C# 6 patterns)
C# 6 does not have nullable reference types (string?). Learn safe null handling: input guards, clear docs, helper Require.NotNull, and contrast with Nullable for value types.
Nullable reference types (awareness) & annotations (C# 6 patterns) is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
NRT awareness & fallback
Idea: Nullable reference types (e.g., string?) add compiler checks in newer C#.
- C# 6 does not support string?
- Use guards, clear docs, and simple helpers
- Return non-null by default
Guard against nulls
Add ArgumentNullException guards for reference-type parameters; then treat values as non-null.
using System;
public static class Greeter
{
// Contract: name must not be null (C# 6 has no string? to enforce)
public static string Hello(string name)
{
if (name == null) throw new ArgumentNullException("name"); // guard
return "Hello " + name.ToUpper(); // safe after guard
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Greeter.Hello("Ada"));
try
{
Console.WriteLine(Greeter.Hello(null)); // will throw (by design)
}
catch (Exception ex)
{
Console.WriteLine("Caught: " + ex.GetType().Name);
}
}
}
Require.NotNull helper
Create a tiny Require.NotNull helper to reduce boilerplate and make intent obvious.
using System;
public static class Require
{
// Works for reference types; returns the validated value
public static T NotNull<T>(T value, string name) where T : class
{
if (value == null) throw new ArgumentNullException(name);
return value;
}
}
public sealed class Email
{
public string Address { get; private set; }
public Email(string address)
{
Address = Require.NotNull(address, "address").Trim();
}
}
public class Program
{
public static void Main(string[] args)
{
Email e = new Email("user@mail.com");
Console.WriteLine(e.Address);
}
}
Nullable<T> vs refs
Nullable<T> (e.g., int?) is for value types. Reference types can already be null; we use guards in C# 6.
using System;
public class Program
{
public static void Main(string[] args)
{
int x = 5; // value type (non-nullable)
int? maybe = null; // Nullable<T> works for value types
Console.WriteLine("x=" + x);
Console.WriteLine("maybe has value? " + maybe.HasValue);
Console.WriteLine("maybe or default: " + (maybe ?? -1));
}
}
Document null contracts
State nullability in XML docs and be consistent: either always non-null or clearly documented when null is possible.
using System;
/// <summary>
/// Parses "key=value" and returns the value part.
/// Returns null if the format is invalid.
/// </summary>
public static class Parser
{
// Explicitly document when null can be returned.
public static string TryGetValue(string line)
{
if (line == null) throw new ArgumentNullException("line");
int idx = line.IndexOf("=");
if (idx < 0 || idx == line.Length - 1) return null; // documented nullable return
return line.Substring(idx + 1);
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Parser.TryGetValue("a=1"));
Console.WriteLine(Parser.TryGetValue("oops")); // prints nothing (null)
}
}
Null-safety checklist (C# 6)
Checklist:
- Guard inputs with ArgumentNullException.
- Return non-null by default; document any nullable returns.
- Use tiny helpers (Require.NotNull) to keep code tidy.
- Prefer overloads that avoid nulls (e.g., empty string instead of null) when sensible.
Null-safety in C# 6
Recap
Recap: NRTs are a newer feature. In C# 6, stay safe with guards, clear docs, helper methods, and predictable non-null return contracts.
Frequently asked questions
Is the “Nullable reference types (awareness) & annotations (C# 6 patterns)” lesson free?
Yes — the full text of “Nullable reference types (awareness) & annotations (C# 6 patterns)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nullable reference types (awareness) & annotations (C# 6 patterns)”?
C# 6 does not have nullable reference types (string?). Learn safe null handling: input guards, clear docs, helper Require.NotNull, and contrast with Nullable for value types. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Nullable reference types (awareness) & annotations (C# 6 patterns)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Generic methods/types; constraints
- Variance (in/out) with interfaces & delegates
- Nullable reference types (awareness) & annotations (C# 6 patterns)