---
title: "How to use Null Coalescing Operator (?) in C#"  
description: "How to use Null Coalescing Operator (?) in C#"  
author: "Anupam Mishra"  
published: 2016-01-24  
updated: 2016-01-24  
canonical: https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 1 minute  

---

# How to use Null Coalescing Operator (?) in C#

Hi Everyone..I want to use [Null](https://www.mindstick.com/forum/159364/difference-between-two-expression-results-with-a-null-value) Coalescing [Operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) (?) in my program. Please give me a better solution

## Replies

### Reply by Shiva Shukla

The ?? operator is also known as the null-coalescing operator. It returns the left side operand if the operand is not null else it returns the right side operand.

1. int? x = null;
2. int y = x ?? 99;
3. Console.WriteLine("The Value of 'Y' is:" + y);
4. string message = "Operator Test";
5. string resultMessage = message ?? "Original message is null";
6. Console.WriteLine("The value of result message is:" + resultMessage);

![How to use Null Coalescing Operator (?) in C#](https://www.mindstick.com/mindstickforums/b71bb542-8afd-43d1-9279-8f32f2dd4c47/images/91b64aef-517e-4c9c-b0dc-a9c20658b1da.png)\
In the preceding example, we have an integer variable "x" that is a nullable type and has a null value so in the result variable "Y" the return value is 99. The same is in the second example. We could check whether the message was null and return an alternate value and result message return "Operator test because message variable has value.\


---

Original Source: https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
