Nullable Type in C#

Опубликовано: 06 Октябрь 2024
на канале: OrBit of the CodinG
14
3

What is nullable type in C# and how to assign a value on it.
value type cannot be assigned a null value. For example, int i = null will give you a compile time error.
C# 2.0 introduced nullable types that allow you to assign null to value type variables.
You can use the '?' operator to shorthand the syntax e.g. int?, long?
int? i = null;
double? D = null;
?? Oprator
Use the '??' operator to assign a nullable type to a non-nullable type.
int? i = null;
int j = i ?? 0;
Console.WriteLine(j);
Null is considered to be less than any value. So comparison operators won't work against null.