In C#, nullable value types are declared using the ?
syntax.
// Define a nullable int value typeint? optionalValue = null;
In C#, nullable types represent the absence of a value and help prevent runtime errors by allowing value types like int?
or bool?
to hold a null
value.
int? optionalValue = null;// Code here that sets optionalValue// Checking if nullable type has a valueif (optionalValue.HasValue){// Execute here if nullable type has a valueConsole.WriteLine(optionalValue.Value); // Output: 5}else{// Execute here if the nullable type has no valueConsole.WriteLine("No value");}
In C#, operators for nullable value types include lifted operators, comparison and equality operators, and logical operators, particularly for the bool?
type.
// Arithmetic operators with nullable typesint? a = 5;int? b = null;int? sum = a + b; // sum is null// Comparison operators with nullable typesint? a = 5;int? b = null;int? c = null;Console.WriteLine(a == b); // false, 5 is not equal to nullConsole.WriteLine(a != b); // true, 5 is not equal to nullConsole.WriteLine(b == c); // true, both are nullConsole.WriteLine(b != c); // false, both are nullConsole.WriteLine(a > b); // false, because b is nullConsole.WriteLine(a < b); // false, because b is nullConsole.WriteLine(b > c); // false, because both are nullConsole.WriteLine(b <= c); // false, comparison with null is always false// Logical operators with nullable types// Logical operators for bool?bool? t = true;bool? f = false;bool? n = null;// Using &Console.WriteLine(t & t); // trueConsole.WriteLine(t & f); // falseConsole.WriteLine(t & n); // nullConsole.WriteLine(f & n); // false// Using |Console.WriteLine(t | f); // trueConsole.WriteLine(f | f); // falseConsole.WriteLine(t | n); // trueConsole.WriteLine(f | n); // null// Note: && and || don't support bool? types// The following lines would cause compilation errors:// Console.WriteLine(t && f);// Console.WriteLine(t || n);
In C#, reference types inherently support nullable values, while value types do not.
// int (a value type) must be declared nullable with "?" to accept a null valueint? ValueType = null;// string (a reference type) inherently accepts a null valuestring ReferenceType = null;
In C#, reference types can be assigned null
by default to avoid null reference exceptions.
// This prevents a compiler warning for a null referencestring? NullString = null;
In C#, implicit and explicit conversions exist between nullable and non-nullable types.
// Explicit conversion of nullable to non-nullable typesint? nullableInt1 = 5;int nonNullableInt1 = (int) nullableInt1;//Implicit conversion non-nullable to nullable typesint nonNullableInt2 = 5;int? nullableInt2 = nullableInt2;
In C#, variable annotations explicitly declare the intended null-state for nullable reference types.
// The compiler reads this reference as "not-null"string NotNull = "";//The compiler reads this reference with the annotation (?) as "maybe-null"string? MaybeNull;//The annotation (!) tells the compiler that the variable is "not-null" supressing warningsMaybeNull = "";int a = MaybeNull!.Length;
In C#, static flow analysis determines the null-state at compile time for nullable reference types.
// Compiler may provide warnings if this variable is used before it's assigned a value.string? NullableString;// After assigning a value, there are no more warnings.NullableString = "Value";
In C#, null-conditional operators (?.
and ?[]
) safely access members of objects that might be null
for both nullable value types and reference types.
// Using the ? null-conditional operator returns null if the refrence is nullstring? message = null;int? incorrectLength = message.Length; // Throws NullReferenceException// Using ? null-conditional operator safely returns nullint? correctLength = message?.Length; // Returns null// It can also be used for indexingchar? firstChar = message?[0];
In C#, the null-forgiving operator (!
) suppresses warnings when the developer knows a value is not null
for both nullable value types and reference types.
// The ! tells the compiler that we know the value is not going to be null, suppressing a warningstring? possiblyNull = GetResult();Console.WriteLine(possiblyNull!.Length); // No warning will occur since ! is used
In C#, the null-coalescing operator (??
) provides default values for nullable types.
int? a = null;int? b = a ?? 3; // b is equal to 3 because a is nulla = 5;b = a ?? 3; // b is now equal to 5
In C#, the System.Nullable<T>
structure includes the properties HasValue
and Value
.
int? optionalValue = 5;// Use the HasValue property to see if value is nullif (optionalValue.HasValue){// Use the Value property to get the valueConsole.WriteLine(optionalValue.Value); // Output: 5}else{Console.WriteLine("No value");}