Introduction
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:
C#
DataTime? dtmVarName = null;
int? intVarName = null;
bool? bVarName = null;
VB.NET
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
C#:
if (dtmVarName.HasValue)
{
//...do something
}
VB.NET:
If dtmVarName.HasValue Then
'...do something
End If
To extract your normal value from your nullable type, use Value property, only if HasValue returns true.
Conclusion
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#:
VB.NET:int? x = 10; or double? d = 4.108;
orDim x As Nullable(Of Integer) = 10Dim 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#:
VB.NET:int j = x.GetValueOrDefault();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#:
VB.NET:Nullable<Nullable<int>> n;Dim n as Nullable(Of Nullable(Of int))

Thavaselvan PalanivelPosted May 6, 2015, 1:17 AM
good one
ashish DevPosted Aug 6, 2010, 8:19 AM
<good> <\good>
azra ansariPosted Aug 24, 2006, 12:14 PM
hi I have a question we are having a .cs class which does a couple of validations on a CSV or XML file in that class there is a method which does this job it doesnt have any parameter i want to test this using NUnit problem is to validate a record we are fetching data from multiple tables in database, so while designing a test case i cant create dynamic values for test data after validation that method inserts those records into a table with specific error codes, i have to test for a particular record there should be a particular error code how can i loop through the records using NUnit, each records may have any Error code. plz reply me ASAP Thanks in advance