breezmacinc.com all in one guide for every developer.

Menu

Nullable Types

In this article, we are going to learn about C# Nullable Types / Nullable Type In C#. Before defining Nullable types, Let’s understand why c# nullable types introduced? what is nullable type in c# and then how to use them?

Why C# Nullable Types?

To understand the answer, why c# nullable types? and follow the problem that nullable type can solve. The c# programming language support two kinds of data types.

Value type (e.g., struct, int, double, etc.)

Reference type (e.g., string, object, etc.)

Assigning a null value to a value is not possible until nullable types were introduced in .NET framework. By taking the advantage of nullable types now value type can store a null value.

A nullable type can hold a correct range of values of a value type (int, boolean etc.) plus NULL. This feature is really helpful when you are working with the database in real-time applications. In a real-time application, there are possibilities that database fields like boolean may need to hold true, false or undefined. Using this nullable type we can solve the problem of undefined.

Declaring Nullable Types In C#

Nullable Types In C# Example

The following example will demonstrate the nullable types usage. number1 assigned to a null value and number2 with 22. Once the code is executed it will give you blank value because of nullable and second with 22 because the value is assigned.