advertisement
The Null-Coalescing (??) Operator in C# - Advanced Concept
Nullable Types
C# provides nullable types to identify and handle value-type fields with null values. Before this feature was introduced, only reference types could be directly assigned null values. Value-type variables with null were introduced, and values were indicated either by using a special value or an additional variable. This additional variable indicated whether or not the required variable was null.
Special values are only beneficial if the decided value is followed consistently across applications. Creating and managing additional fields for such variables leads to more memory space and becomes tedious. These problems are solved by the introduction of nullable types.
1. Creating Nullable Types
A nullable type is a means by which null values can be defined for the value types. It indicates that a variable can have the value null. Nullable types are instances of the System. Nullable<T> structure.
A variable can be made nullable by adding a question mark following the data type. Alternatively, it can be declared using the generic Nullable<T> structure present in the System namespace.
2. Characteristics
Nullable types in C# have the following characteristics:
- They represent a value type that can be assigned a null value.
- They allow values to be assigned in the same way as that of the normal value types.
- They return the assigned or default values for nullable types.
- When a nullable type is assigned to a non-nullable type and the assigned or default value has to be applied, the ?? operator is used.
3. Implementing Nullable Types
A nullable type can include any range of values valid for the data type to which the nullable type belongs. For example, a bool type that is declared as a nullable type can be assigned the values true, false, or null. Nullable types have two public read-only properties that can be implemented to check the validity of nullable types and to retrieve their values.
These are as follows:
The HasValue Property:-
HasValue is a bool property that determines the validity of the value in a variable. The HasValue property returns a true if the value of the variable is not null, else it returns a false.
The Value Property:-
The Value property identifies the value in a nullable variable. When the HasValue evaluates to true, the Value property returns the value of the variable, otherwise it returns an exception.
4. The ?? Operator
A nullable type can either have a defined value or the value can be undefined. If a nullable type contains a null value and this nullable type is assigned to a non-nullable type, the compiler generates an exception called system. InvalidOperationException.
To avoid this problem, a default value can be specified for the nullable type that can be assigned to a non-nullable type using the ?? operator. If the nullable type contains a null value, the ?? operator returns the default value.
5. Converting Nullable Types
C# allows any value type to be converted into a nullable type or a nullable type into a value type. C# supports two types of conversions on nullable types:
- Implicit conversion: The storing of a value type into a nullable type is referred to as implicit conversion.
- Explicit conversion: The conversion of a nullable type to a value type is referred to as explicit conversion.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0