Join us on the demo, while our product experts provide a detailed walkthrough of our enterprise platform.

logoAsync

The latest news in the world of engineering.

Identity and Access Management (IAM), including security and customer experience.

Visit Blog
Fuel

Grow your business to millions.Engage and retain your customers.

Visit Blog

How to Work with Nullable Types in C#

Nullable is a term in C# that allows an extra value null to be owned by a form. We will learn in this article how to work with Nullable types in C#.

Hemant Manwani
Hemant Manwani
January 29, 2021
3 min read

Free, Secure and Trusted Way to Authenticate Your Visitors

Add login to your website in 5 minutes completely for free!

Free Sign UpNo hidden costs. No credit card needed.

In C#, We have majorly two types of data types Value and Reference type. We can not assign a null value directly to the Value data type. In this case, C# 2.0 provides us the Nullable types to assign a value data type to null.

What is Nullable types?

As described above, The Nullable types used to assign the null value to the value data type. That means we can assign a null value directly to a variable of the value data type. We can declare null value using Nullable<T> where T is a type like an int, float, bool, etc.

Nullable types represent the Null value as well the actual range of that data type. Like the int data type can hold the value from -2147483648 to 2147483647 but a Nullable int can hold the value null and range from -2147483648 to 2147483647

How to declare Nullable types

There are two ways to declare Nullable types.

Nullable<int> example;

OR

int? Example;

Properties of Nullable types

Nullable types have two properties.

  1. HasValue
  2. Value

HasValue: This property returns a bool value based on that if the Nullable variable has some value or not. If the variable has some value, then it will return true; otherwise, it will return false if it doesn’t have value or it’s null.

Nullable<int> a = null;
Console.WriteLine(a.HasValue); // Print False
Nullable<int> b = 9;
Console.WriteLine(b.HasValue); // Print True

Value: This property gives the value of the Nullable type variable. If the variable has some value, it will return the value; else, it will give the runtime InvalidOperationException exception when the variable value is null.

Nullable<int> a = null;
Console.WriteLine(a.Value); // Gives run time exception of type 'InvalidOperationException'
Nullable<int> b = 9;
Console.WriteLine(b.Value); // Print 9

Method of Nullable types

GetValueOrDefault(): This method returns the actually assigned value of the Nullable type variable if the value is not null, and if the variable value is null, then it will give the default value of that data type. Here is the example code

Nullable<int> a = 9;
Console.WriteLine(a.GetValueOrDefault()); // Returns 9
Nullable<int> a = null;
Console.WriteLine(a.GetValueOrDefault()); // Returns 0

Rules of using Nullable types:

To use the Nullable type as a local variable, it should be declared first; it will give a compile-time error. This rule is similar to the value data type.

int? b;
Console.WriteLine(b.Value); //Compile time error 'use of unassigned local variable b'

If the Nullable variable is a property in a class and after that, if we are accessing that Nullable variable, then it will not give any error because, in the class variable, it is declared as null automatically.

class Test
{
     public int? B;
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new Test().B); // No compile time error
    }
}

null coalescing operator (??)

We can not assign the Nullable type variable value to the non-nullable type variable directly. As in the example below, if we try to assign the value, it will give the compile-time error.

int? a = 10;
int b = a;//Compile time error `Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)`

Note: We can use compare operators == and != operator with Nullable type variables and non Nullable type variables.

For the Nullable variable, we can use the null coalescing operator (??) to check if the variable value is null or not. Then we can assign the non-nullable type variable value according to that. This operator can be used when we are unsure that at run time if that Nullable variable's value is changed according to our logic or not. Here is an example of that

int? a=null;
int b=a?? 10;
Console.WriteLine(b);  // Prints 10

In the above example, if the variable a value is null,, it will assign the value 10 to b variable. In that case, b is assigned with value 10, and the printed value will be 10.

Conclusion

In this article, We have discussed the Nullable types, and it's properties and methods. The main advantage of using the Nullable types is that we can store the Null value in a column of a database using this type. If you want to learn more about C# here is an article written by me on How to Use Enum in C#



LoginRadius Docs

Implement Authentication in Minutes

click here

Most Popular Tags

EngineeringJavaScriptReactNodeJsAuthenticationCSSSecurityC#OAuthGo

Do you want a free authentication solution?

Add the world's most secure, reliable and easy to implement user authentication solution on your applications at $0Get Started Free

Hemant Manwani

Hemant Manwani

Hemant Manwani is a Software Engineer at LoginRadius who is interested in how the web works and developer tools that make working with web easier.

View Profile

Try a Modern Authentication Solution

$0/ month

Free Sign Up
  • 7,000 Monthly Active Users
  • 1 Web or Mobile App
  • Email/Password Login
  • 3 Social Login Providers
  • User Data Storage
  • Transactional Email Template
  • Customizable Login Interfaces