C# Nullable Types Null Checking And Coalescing Operators

C# Code Snippets Nullable types Null Checking And Coalescing Operators


In this code snippetwe will take a look at the nullable types, null coalescing and checking operators in C#.

Reference types are nullable by default, value types like int for example are not. You can make them nullable by appending ? after the data type declaration like so: int? 

If you want to make reference type non-nullable by default you can configure that in your project’s config file. This can help you avoid the all too familiar NullReferenceExceptionThen ? has to be applied to make them nullable(just like value types). 

Null checking and coalescing operators(??, ??= and ?) allow you to deal with null values in a more compact and concise way than using if statements to check if a variable/property/object is null and then access/set its value or members.

Let’s have a look at the code below to see how to use nullable types, null coalescing and checking operators.


/**************** Nullabe Value Types ****************/

//A string is nullable.
string myString = null;

//This would give you an error because int is not nullable.
//int myInt = null;  

//You can make a non-nullable type nullable by using ? like so:
int? myInt = null;


/************** Nullabe Reference Types **************/

//Reference types are nullable by default. This can be changed by adding enable to the config file.
//Now all the reference types can't be set to null by default. 

//Now something like this would give us a warning.
string myNotNullableString = null;

//You can make non-nullable reference types nullable again by using ? like so:
string? myNullableString = null;


/************* null coalescing operators *************/

//If myInt is not null it will be converted to a non-nullable int else 0 will be returned
int myNotNullInt = myInt ?? 0;

//??= can be used to check if a variable is null and assign a specified value to it if that is the case. 
myInt ??= 15;


/************* Null conditional operator *************/

var myClass = new MyClass();

//This throws a null reference exception because MyOtherClassProperty will be set to null.
//if (myClass.MyOtherClassProperty.MyProperty > 10) 
//    Console.WriteLine("Number is bigger than 10.");

//Usually you would solve this by checking that MyOtherClassProperty is not null and then try to access its members.
//This can add a lot of extra null checks especially if you have many nested objects. 

//The ? null check operator makes it much more compact to check and make sure MyOtherClassProperty isn't null.
if (myClass.MyOtherClassProperty?.MyProperty > 10) //If MyOtherClassProperty happens to be null the if will simply be false.
    Console.WriteLine("Number is bigger than 10.");

class MyClass
    public MyOtherClass MyOtherClassProperty { get; set; } = null; //MyOtherClassProperty will be set to null by default.

    public class MyOtherClass
        public int MyProperty { get; set; }


Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger