Nullable in Calculations
Understand how nulls propagate through arithmetic.
Lifted Operators
C# "lifts" arithmetic and comparison operators to work on nullable types. Operators like + automatically gain a nullable version.
using System;
class Program {
static void Main() {
int? a = 5;
int? b = 3;
Console.WriteLine(a + b);
}
}Null Propagates Through Math
If any operand in a lifted arithmetic expression is null, the result is null.
using System;
class Program {
static void Main() {
int? a = 5;
int? b = null;
int? sum = a + b;
Console.WriteLine(sum.HasValue);
}
}All lessons in this course
- Introducing Nullable Value Types
- HasValue and Value Properties
- Null-Coalescing Operators
- Nullable in Calculations