Ganesan Senthilvel
Ganesan Senthilvel

Software engineers need to be very mindful of Null values. Null is conceptually different from zero; it was left blank during record creation. It is a tricky data element in the fundamentals of computing. Let us learn with a few smart questions.

From a database perspective, Null (or NULL) is a special marker used in Structured Query Language to indicate that a data value does not exist in the database.

What?

C# has a class called Nullable, which can hold either an actual value or null. There are two ways to use this type:

  1. Explicit Syntax
    Nullable<data_type> variable_name = null;

  2. Shortcut using ? operator
    datatype? variable_name = null;

Sample C# code to declare a Nullable object:

/// <comment>
/// Using method 1 : explicit syntax
/// </comment>
Nullable<int> Age = null;
/// <comment>
/// Using method 2 using ?
/// </comment>
int? Age = null;

This allows us to assign a null value to Age. If Age was a traditional integer, the compiler would have given an error.

Nullable is an instance of the System.Nullable struct, where T is a Generic type to hold any non-nullable value types like integer , floating-point, boolean, etc. Nullable holds -2147483648 to 2147483647 or null Nullable holds true, false, or null

Why?

While Nullable can be useful in a variety of contexts, let’s use an example based in databases.

Let’s say we have an Associates table containing ID, Name, Age and Course as integer, string, integer and string respectively. Let’s say that we have a requirement that users can provide their age, but they don’t have to.

image

A naive approach would be to store either a 0 or negative number to handle this edge case. While this technically works, it’s not elegant and anyone working with the data needs to know about the special values. Nullable is beneficial here to indicate that Kashif didn’t provide any information.

How?

A Nullable object has the useful attribute HasValue to tell us if it actually holds non-null data.

/// <comment>
/// Sample C# program to demonstrate
/// the usage of HasValue attribute
/// </comment>
class TestHasValue {
static void Main()
{
/// <comment>
/// Declare nullable ModelYear
/// with initial value of null
/// </comment>
Nullable<int> ModelYear = null;
/// <comment>
/// On assigning null value, system
/// will return false
/// </comment>
// check the value of object
Console.WriteLine(ModelYear.HasValue);

/// <comment>
/// Declare nullable ModelMonth
/// with initial value of 12
/// </comment>
Nullable<int> ModelMonth = 12;
/// <comment>
/// On assigning valid value of 12,
/// system will return true.
/// If the object is not assigned with any value
/// then the system will throw compile time error
/// </comment>
// check the value of object
Console.WriteLine(ModelMonth.HasValue);
}
}

The GetValueOrDefault(T) method fetches the assigned value from non-null content. If the Nullable integer object holds a null, the provided default value of zero is returned.

/// <comment>
/// Sample C# program to demonstrate
/// the usage of GetValueOrDefault method
/// and null-coalescing operator
/// </comment>
class TestCoalescing {
static void Main()
{
/// <comment>
/// Declare nullable NullModelMonth
/// with initial value of null
/// </comment>
Nullable<int> NullModelMonth = null;
/// <comment>
/// For null, system output is zero by default
/// using GetValueOrDefault method
/// </comment>
// check the value of object
Console.WriteLine(NullModelMonth.GetValueOrDefault());

/// <comment>
/// If NullModelMonth has null, then
/// 10 is assigned to ModelMonth
/// using null-coalescing operator ??
/// </comment>
int ModelMonth = NullModelMonth ?? 10;
}
}

Happy Nullable Coding!