Nullable Types
Added in .NET 2.0
Nullable types can only be used with value types.
Nullable types can represent all the values of an underlying type plus an additional null value.
System.Nullable<T> variable;
T can be any value type, including struct
T cannot be a referenced type
Every nullable type has two public readonly properties
HasValue - false when variable contains a null value
Value - the value when variable does not contain a null value
Use value property to get the value of nullable type.
Use HasValue property to check whether value is assigned to nullable type or not.
You can only use == and != operators with a nullable type.
Shorthand Syntax
You can use the '?' operator to shorthand the syntax e.g. int?, long? instead of using Nullable<T>.
T? variable;
int? myInteger = null;
double? myDouble = null;
System.DateTime? myDateTime = null;
Only Value Types
Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.
String is already a nullable type.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext