How Do You Identify Nullable Type in C# (C Sharp)

Can you assign null to an integer variable? Not in other languages. But you can do it in C# using nullable types. Not just integer, any value types can accept null value if it is declared as nullable type. When you declare it as nullable, in addition to the range of values that your value type can accept it also accepts null value. Consider the following example:

class sampleClass {
public static void Main() {
int intVar = null;
Console.WriteLine(“Value of intVar is {0} “, intVar);
}
}
This code is not legal since integer variables cannot accept null values. Hence it will throw InvalidOperationException. However you can make this code work by declaring intVar as nullable type. Here is the modified version of the code:
class sampleClass {
public static void Main() {
int? intVar = null;
Console.WriteLine(“Value of intVar is {0} “, intVar);
}
}

Output of this code will be:

Value of intVar is null

You declare the integer variable intVar as nullable type by associating “?” symbol after value type but before the variable name. The corresponding code is highlighted in bold in the example shown above.

How to Identify Nullable Type?

When you browse through the code, you can identify nullable type by looking at the “?” symbol. What if you have to identify nullable type during execution of your code to decide on further processing? Here are the ways:

• Use typeof operator

• Use the namespace System.Reflection and its corresponding methods

Identify Nullable Type Using typeof Operator:

To know how you can identify nullable type using typeof operator, browse through the code sample given below:

class sampleClass {/
public static void Main() {
int? intVar = 100;
Type typeOfVar = typeof(intVar);
bool isGeneric = typeOfVar.IsGenericType;
if(isGeneric && typeOfVar.GetGenericTypeDefinition() == typeof(Nullable<>)) {
Console.WriteLine(“intVar is of type nullable”);
}
else {
Console.WriteLine(“intVar is not a nullable type”);
}
}
}

Output of this code will be:
intVar is of type nullable

In this example you have a nullable type variable called intVar. You first fetch the type of intVar using typeof operatogr and assign it to a System.Type variable called typeOfVar. You then check if the type retrieved is a generic type. If so, you check if it is nullable by checking its definition against:

typeof(Nullable<>)
If the condition returns true then your variable belongs to a nullable type.

Identify Nullable Type Using System.Reflection Namespace:

Assume that you have an internal class called employee containing certain attributes. You have to display its attributes which are of nullable type. How will you do it? You can do it using the methods of System.Reflection namespace. Here is the code sample for it:

internal class employee {
int? empNo;
string empName;
}
class sampleClass {
public static void Main() {
employee emp = new employee();
System.Reflection.FieldInfo[] empFields = typeof(emp).GetFields();
foreach(System.Reflection.FieldInfo empField in empFields) {
if(empField.IsGenericType && empField.FieldType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
Console.WriteLine(“Field {0} of employee is nullable”,
empField.Name);
}
}
}
}

How Not to Identify Nullable Type?

In addition to understanding above mentioned ways of identifying nullable type, you should also be aware of the following ways which will not help you in identifying nullable type but still they might mislead you:

• Using GetType method of System.Type

• Using is Operator

Both these ways are used to identify the type of the variable, but they will not predict if your variable is nullable. To make it clear, consider the following example:

class sampleClass {
public static void Main() {
int? intVar = 500;
Type typeOfVar = intVar.GetType();
Console.WriteLine(“Type of intVar is :” + typeOfVar.FullName);
}
}
Output of this code will be:

Type of intVar is System.Int32

You get such an output because GetType() method determines only the value type associated with a particular variable. It cannot identify if the variable is nullable or not. Similarly using is operator will also check for the value type associated with the variable. And it doesn’t bother about the nullable nature of the variable. Given below is an example of is operator:

class sampleClass {
public static void Main() {
int? intVar = 500;
if( intVar is int) {
Console.WriteLine(“intVar is an integer variable”);
}
else {
Console.WriteLine(‘intVar is not an integer variable’);
}
}
}

Output of this code will be:

intVar is an integer variable

Hence always remember that GetType() method as well as is operator will not identify nullable type. They identify only the associated value type.

| AppDomain class in .Net | Asynchronous programming in .Net | Better data transmission using Compressed Streams in .Net | Encoding and Decoding Data in .Net | Handling Unmanaged Code in .Net | How are Generics of C# Different from C++ Templates | How Do You Establish Variance in Generic Typesm: Reference Types | How Do You Identify Nullable Type in C#m: Reference Types | How Do You Perform Boxing On Nullable Types in C# |Overview of Code Access Security in .Net |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.