Have you ever needed to add a null value to your integer, date & time,decimal or Boolean etc… data types?! I’m sure you may faced this desire before and you had to work around it to submit null value to those value data types. For example using DateTime.MinValue as a mark to a null value. But you cannot submit DateTime.MinValue to SQL Server Database, as this will throw an exception.
So .Net Framework 2.0 comes out with a great new feature and solution to your problem. It is The Nullable Value Types. I’m saying .Net Framework to generalize the feature as it is a new feature in both C# and VB.NET.The New Story:
In .Net Framework 2.0, value types can be extended to take either their normal values or a null value. Such an extension is called a nullable type.
Nullable type is constructed from the generic Nullable structure. By having this feature, you can submit null values to you database table field normally, like submitting a null DateTime value to a datetime field in your database table.
Now how can we declare a nullable value type?
Simply like this:
DataTime? dtmVarName = null;
int? intVarName = null;
bool? bVarName = null;
Dim dtmVarName As Nullable(Of DateTime)
Dim intVarName As Nullable(Of Integer)
Dim bVarName As Nullable(Of Boolean)
How can you check if your nullable value type contains data?
Simply by checking HasValue property attached to your nullable value type
If dtmVarName.HasValue Then
To extract your normal value from your nullable type, use Value property, only if HasValue returns true.
The following is written in MSDN, but I found it very useful to be used to close this article. Nullable types have the following characteristics:
- Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)
- The syntax T? (in C#) is shorthand for System.Nullable<T>, where T is a value type. The two forms are interchangeable.
Assign a value to a nullable type in the same way as for an ordinary value type, for example:
C#: int? x = 10; or double? d = 4.108;
VB.NET: Dim x As Nullable(Of Integer) = 10 or Dim d As Nullable(Of Double) = 4.108
- Use the System.Nullable.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example
C#: int j = x.GetValueOrDefault();
VB.NET: Dim j as Integer = x.GetValueOrDefault()
- Use the HasValue and Value read-only properties to test for null and retrieve the value.
- The HasValue property returns true if the variable contains a value, or false if it is null.
- The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown.
- The default value for a nullable type variable sets HasValue to false. The Value is undefined.
- Use the ?? (C#) operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;
- Nested nullable types are not allowed. The following line will not compile:
C#: Nullable<Nullable<int>> n;
VB.NET: Dim n as Nullable(Of Nullable(Of int))