Understand Nullable type in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2202 red flag

How to use Nullable variable in C#.

Understand Nullable type in C#

In this article we will learn how to understand Nullable type in C#. Nullable types are instance of the Nullable structure. If we define one variable as a Nullable variable then it will capable to store null value along with its default value.

For example if we define one Boolean variable as Nullable type then it can store 3 values, they are true, false and null.

Let’s define one Boolean variable and try to assign null value into it. See in below code.


We are see that compiler is complaining when we are trying to assign null to normal Boolean variable. So, the solution is we have to declare the variable as Nullable. 


So, now the error has gone because we have defined Boolean variable as Nullable.  Now, let’s check it how it’s work? We will implement very simple code and will check it within if condition.

public static void Main(String[] args)
            {
                bool? a = null;
                if (a == null)
                {
                    Console.WriteLine("Value is null");
                }
                else
                    Console.WriteLine("Value is not null");
                Console.ReadLine();
            }

Here is sample output.

We can define any variable as Nullable type. Here is example.

            public static void Main(String[] args)
            {
                bool? a = null;
                int? b = null;
                float? c = null;
                char? d = null;
                Console.ReadLine();
            }

Here we have defined all primitive variables as Nullable.

Characteristic and uses of Nullable type:-

1)   Nullable types represent value-type variable that can be assign the value of null. We cannot define reference type as Nullable type because by default they accept null.

2)  The ability to assign null to numeric null to numeric and Boolean type is especially useful when we are dealing with database and other data types that contain elements that may not be assigned value. For example one Boolean field in database may contain true, false or null (means not defined).

3)  Assigning a value to Nullable type is just like normal variable. Like int?a =100;

Conclusion:-
In this article we have learned how to use Nullable type in C#. Hope you understood the concept.


 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)