Nullable types in C# .NET allows assigning null values to structures. By declaring a structure as nullable type, null value can be assigned to structure variable.
In this tutorial, let's understand more about nullable types, by going through the below sections:
- Why nullable types?
- How to declare nullable types?
- Working with APIs of nullable types with examples
- How .NET has defined nullable types?
Why nullable types?
Let's consider an example of developing a Customer management application; The application stores the Customers details including Date of Birth and Annual Salary. As per business requirement, a record can exist with no values for the Date of Birth and Annual Salary fields. So when it comes to DB design, the columns to store these two fields will be nullable.
While designing app layer contracts, you will start thinking about business entities in the system. The Customer entity class has Date of Birth and Annual Salary properties. So you declare Date of Birth as DateTime & Annual Salary as Integer type. Both DateTime and Integer are Structures and they cannot have null values. But application needs these two fields to have null when a customer hasn't specified values. But null value is not supported for structure data types.
{
public DateTime DateOfBirth {get;set;}
public int AnnualSalary {get;set;}
}
So there is a problem. This problem can be workaround by assigning zero to Salary and the corresponding interpretation in UI layer to display Not Specified when value is zero.
The workaround for DateTime field is not easy. The default value for DateTime structure in .NET is less than the allowed value in MS SQL server database DateTime column. So you need to do more workaround which isn't straightforward and your fellow developers will have a difficult time in understanding such code and hence code will become less maintainable. So these workarounds are not clean design.
These problems can be solved by making .NET structure fields to have a null value. This can be achieved using nullable structure types. Then, let's see how?
How to declare nullable types?
Nullable types declared with "question mark - ?" suffixed to the structure data type like below:
public int? AnnualSalary {get;set;}
Working with APIs of nullable types with examples
Nullable types notably has two properties HasValue and Value and GetValueOrDefault method with two overloads.
- HasValue: HasValue boolean property indicates whether nullable type has any value other than null.
DateTime? DateOfBirth {get;set;}
if(DateOfBirth.HasValue) {
// Nullable type has valid value.
} - Value: Value is a property of underlining structure type. Value is read-only property, you cannot assign any value directly to this.
DateTime? DateOfBirth {get;set;}
if(DateOfBirth.HasValue)
{
DateTime value = DateOfBirth.Value;
} - GetValueOrDefault: This method on the Nullable type returns the underlining structure value if has value else returns the default value of the underlining structure type.
int? annualSalary;
// This returns zero; because
// integer value type default value is zero
int salary = annualSalary.GetValueOrDefault();
// Overloaded method taking default value
// to return if nullable type value is null.
salary = annualSalary.GetValueOrDefault(1213)
How .NET has defined nullable types
In mscorlib.dll assembly Nullable types are declared as structure with Generics
{
}
Reference types like classes cannot be declared as nullable. If you do so, you will get the below error:
No comments:
Post a Comment