Nullable Types and Null Safety

Three-valued logic: Nothing in nullable comparisons yields Nothing (treated as False in If). Comparing discount = 0 when discount is Nothing isn't True.

VB has no nullable reference type annotations. Be explicit:

Validate inputs with guard clauses.

Return empty collections, not Nothing.

Use the TryX(ByRef) pattern.

Consider a Maybe(Of T) type for optional results.

Interop: nullable annotations in C# libraries are ignored by VB (no warnings), so read API docs carefully.

Dim discount As Decimal? = Nothing
Dim final = price - discount.GetValueOrDefault()
Dim label = If(customer?.Name, "(guest)")
Dim zip = order?.Customer?.Address?.Zip
Dim firstLine = order?.Lines?(0)
ArgumentNullException.ThrowIfNull(order)
If discount.HasValue AndAlso discount.Value > 0 Then ...