In C#, the compiler does not allow you to assign a null value to a variable. So, C# 2.0 provides a special feature to assign a null value to a variable that is known as the Nullable type. The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type.
The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value. This helped us to tackle the issue of NullReferenceException without using conditionals. In this article, the discussion revolves around the nullable types for value types.
The Nullable type is an instance of System.Nullable<T> struct. Here T is a type which contains non-nullable value types like integer type, floating-point type, a boolean type, etc. For example, in nullable of integer type you can store values from -2147483648 to 2147483647, or null value.
Syntax:
Nullable<data_type> variable_name = null;
Or you can also use a shortcut which includes ? operator with the data type.
datatype? variable_name = null;
Example:
// this will give compile time error
int j = null;
// Valid declaration
Nullable<int> j = null;
// Valid declaration
int? j = null;
How to access the value of Nullable type variables?
You cannot directly access the value of the Nullable type. You have to use GetValueOrDefault() method to get an original assigned value if it is not null. You will get the default value if it is null. The default value for null will be zero.
Example:
C#
// C# program to illustrate Nullable Types
using System;
class Geeks {
// Main Method
static void Main(string[] args)
{
// defining Nullable type
Nullable<int> n = null;
// using the method
// output will be 0 as default
// value of null is 0
Console.WriteLine(n.GetValueOrDefault());
// defining Nullable type
int? n1 = null;
// using the method
// output will be 0 as default
// value of null is 0
Console.WriteLine(n1.GetValueOrDefault());
// using Nullable type syntax
// to define non-nullable
int? n2 = 47;
// using the method
Console.WriteLine(n2.GetValueOrDefault());
// using Nullable type syntax
// to define non-nullable
Nullable<int> n3 = 457;
// using the method
Console.WriteLine(n3.GetValueOrDefault());
}
}
Output:
0
0
47
457
Characteristics of Nullable types
- With the help of nullable type you can assign a null value to a variable without creating nullable type based on the reference type.
- In Nullable types, you can also assign values to nullable type. As shown in the below example.
Example:
C#
// C# program to illustrate the
// use of Nullable type
using System;
class GFG {
// Main Method
static public void Main()
{
// a is nullable type
// and contains null value
int ? a = null;
// b is nullable type int
// and behave as a normal int
int ? b = 2345;
// this will not print
// anything on console
Console.WriteLine(a);
// gives 2345 as output
Console.WriteLine(b);
}
}
Output:
2345
- You can use Nullable.HasValue and Nullable.Value to check the value. If the object assigned with a value, then it will return "True" and if the object is assigned to null, then it will return "False". If the object is not assigned with any value then it will give compile-time error.
Example:
C#
// C# program to illustrate the
// use of Nullable<L>.Hasvalue
using System;
class GFG {
// Main Method
static void Main()
{
// a is nullable type
// and contains null value
Nullable<int> a = null;
// check the value of object
Console.WriteLine(a.HasValue);
// b is nullable type
// and contains a value
Nullable<int> b = 7;
// check the value of object
Console.WriteLine(b.HasValue);
}
}
Output:
False
True
- You can also use == and ! operators with nullable type.
- You can also use GetValueOrDefault(T) method to get the assigned value or the provided default value, if the value of nullable type is null.
- You can also use null-coalescing operator(??) to assign a value to the underlying type originate from the value of the nullable type.
Example:
C#
// C# program to illustrate the
// use of null-coalescing operator(??)
using System;
class GFG {
// Main Method
static public void Main()
{
// a is nullable type
// and contains null value
int ? a = null;
// it means if a is null
// then assign 3 to b
int b = a ?? 3;
// It will print 3
Console.WriteLine(b);
}
}
Output:
3
- Nullable types do not support nested Nullable types.
- Nullable types do not support var type. If you use Nullable with var, then the compiler will give you a compile-time error.
Advantage of Nullable Types:
- The main use of nullable type is in database applications. Suppose, in a table a column required null values, then you can use nullable type to enter null values.
- Nullable type is also useful to represent undefined value.
- You can also use Nullable type instead of a reference type to store a null value.
Similar Reads
C# Data Types
Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# String IsNullOrEmpty() Method
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned ââ or String.Empty (A constant for empty strings).Example 1: Using IsNullOrEmpty
2 min read
C# | Type.IsArrayImpl() Method
Type.IsArrayImpl() Method is used when overridden in a derived class, implements the IsArray property and determines whether the Type is an array. Syntax: protected abstract bool IsArrayImpl (); Return Value: This method returns true if the Type is an array otherwise, false. Below programs illustrat
3 min read
Boolean Data Type
In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depend
13 min read
C# | Type.IsAssignableFrom(Type) Method
Type.IsAssignableFrom(Type) Method is used determine whether an instance of a specified type can be assigned to a variable of the current type. Syntax: public virtual bool IsAssignableFrom (Type c); Here, it takes the type to compare with the current type. Return Value: This method returns true if a
2 min read
C# | IsNullOrWhiteSpace() Method
In C#, IsNullOrWhiteSpace() is a string method. It is used to check whether the specified string is null or contains only white-space characters. A string will be null if it has not been assigned a value or has explicitly been assigned a value of null. Syntax: public static bool IsNullOrWhiteSpace(S
2 min read
C# | Type.IsEnumDefined() Method
Type.IsEnumDefined(Object) Method is used to return a value which indicates whether the specified value exists in the current enumeration type. Syntax: public virtual bool IsEnumDefined (object value); Here, it takes the value to be tested. Return Value: This method returns true if the specified val
3 min read
C# | Type.GetTypeHandle() Method
Type.GetTypeHandle() Method is used to get the handle for the Type of a specified object. Syntax: public static RuntimeTypeHandle GetTypeHandle (object o); Here, it takes the object for which to get the type handle. Return Value: This method returns The handle for the Type of the specified Object. E
2 min read
PostgreSQL - IS NULL operator
The PostgreSQL IS NULL operator is used to check whether a value is NULL. In the context of databases, NULL indicates that data is either missing or not applicable. Since NULL cannot be compared directly with any integer or string (as such comparisons result in NULL, meaning an unknown result), the
2 min read
Null in Python
In Python, None represents the absence of a value and is the only instance of the NoneType. It's often used as a placeholder for variables that don't hold meaningful data yet. Unlike 0, "" or [], which are actual values, None specifically means "no value" or "nothing." Example:Pythona = None if a is
3 min read