---
title: "How to use null-coalescing operator?"  
description: "How to use null-coalescing operator?"  
author: "Tarun Kumar"  
published: 2017-07-04  
updated: 2019-06-19  
canonical: https://www.mindstick.com/forum/34318/how-to-use-null-coalescing-operator  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to use null-coalescing operator?

How to use ?? [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp)-coalescing [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) in c# . I have [never](https://yourviews.mindstick.com/view/290/zoos-prisons-for-beings-who-have-never-committed-a-crime) used ?? operator. And How we can assign [default value](https://www.mindstick.com/forum/2035/default-value-when-using-singleordefault) when it is null using ?? Operator.

Please help

## Replies

### Reply by Shadman Kudchikar

The ?? operator is called the null-coalescing operator. You can use it to provide a [default](https://www.mindstick.com/interview/12771/what-is-the-importance-of-default-resources) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) for nullable value types or for reference types. The operator returns the left value if it’s not null; otherwise, the right operand. Here is an example of using the operator.

```
int? x = null;int y = x ?? -1;
```

In this case, the value of y is -1 because x is null. You can also nest the null-coalescing operator, below is an example,

```
int? x = null;
int? z = null;
int y = x ??
        z ??
        -1;
```

Of course, you can achieve the same with an if statement but the null-coalescing operator can shorten your code and improve its readability.

You can learn more about Null Coalescing Operator C# here.

### Reply by Sushant Mishra

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise, it returns the right hand operand.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{    class Program    {        static int? GetNullableInt()        {            return null;        }         static string GetStringValue()        {            return "Mindstick Softwares";        }         static void Main()        {                         int? x = null;             // Set y to the value of x if x is NOT null; otherwise,            // if x = null, set y to 5.            int y = x ?? 5;             // Assign i to return value of the method if the method's result            // is NOT null; otherwise, if the result is null, set i to the            // default value of int.            int i = GetNullableInt() ?? default(int);             string m = GetStringValue();            // Display the value of s if s is NOT null; otherwise,             // display the string "Unspecified".           
Console.WriteLine(m?? "Unspecified");            Console.WriteLine(i);            Console.ReadKey();        }    }} 
```


---

Original Source: https://www.mindstick.com/forum/34318/how-to-use-null-coalescing-operator

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
