Nullable Data Types in C-Sharp / C#

A value type variable cannot be null. That's why they are called Value Type. 
 Value type has a lot of advantages, however, there are some scenarios where we require value type to hold null also. For instance, If you are retrieving nullable integer column data from database table, and the value in database is null, there is no way you can assign this value to an C# int.

The null values are very critical while working with relational databases, Since it is quite common to encounter undefined columns in RDBMS tables.
Without the concept of nullable data types, there is no convinient manner in c# to represent a numeriacal data point value with no value.

C# data types have a fixed range and are represented as a type in the System namespace. For example, the System.Boolean data type can be assigned a value from the set {true, false}.

Since all of the numerical data types as well as the Boolean data type are value types, it can never be assigned the value of null, as that is used to establish an empty object reference.

For example :

Code Snippet
  1. static void Main(string[] args)
  2. {
  3. bool MyBool = null;
  4. int MyInt = null;
  5. // Compiler errors!
  6. // Value types cannot be set to null in C#.
  7. string MyString = null;
  8. // Strings are reference types in C#, Hence can be null.
  9. }

C# supports the concept of nullable data types.
Simply put, a nullable type can represent all the values of its underlying type, plus the value null.
Thus, if we declare a nullable bool variable, it could be assigned a value from the set {true, false, null}.

To define a nullable variable type, the question mark symbol (?) is suffixed to the underlying data type.

"Note that this syntax is only legal when applied to value types. If you attempt to create a nullable reference type (including strings), you are issued a compile-time error. Like a nonnullable" variable, local nullable variables must be assigned an initial value before you can use them.

Have a look at an example below:

Code Snippet
  1. static void Main(string[] args)
  2. {
  3. // Define some local nullable variables.
  4. int? NullableInt = 10;
  5. double? NullableDouble = 3.14;
  6. bool? NullableBool = null;
  7. char? NullableChar = 'a';
  8. int?[] arrayOfNullableInts = new int?[10];
  9. string? s = "oops"; // Error! Strings are reference types!
  10. }

In C#, the ? suffix notation is a shorthand for creating an instance of the generic System.Nullable<T> structure type.

Nullable data types can be particularly useful when you are interacting with databases, given that columns in a data table may be intentionally empty (e.g., undefined).
For example :

Code Snippet
  1. // Note the nullable return type.
  2. public int? GetIntFromDatabase()
  3. {
  4. return numericValue;
  5. }

The above function may return a null value since the way coming from the database may be null.
Take a look at the code below which is going to call this function.

Code Snippet
  1. {
  2. DatabaseReader dr = new DatabaseReader();
  3. // Get int from "database".
  4. int? i = dr.GetIntFromDatabase();
  5. }

Post a Comment