---
title: "How useful is C#'s ?? operator?"  
description: "How useful is C#'s ?? operator?"  
author: "Anonymous User"  
published: 2014-01-29  
updated: 2014-01-29  
canonical: https://www.mindstick.com/forum/1917/how-useful-is-c-sharp-s-operator  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How useful is C#'s ?? operator?

I have been intrigued by the ?? [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server), but have still been unable to use it. I usually think about it when I am [doing something](https://www.mindstick.com/forum/33390/nsdateformatter-am-i-doing-something-wrong-or-is-this-a-bug) like:

[var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) x = (someObject as someType).someMember;

If someObject is valid and someMember is [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp), I could do

var x = (someObject as someType).someMember ?? defaultValue;

but almost invariably I get into problems when someObject is null, and ?? doesn't help me make this any cleaner than doing the null [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) myself.

What uses have you guys found for ?? in [practical](https://answers.mindstick.com/blog/54/minimal-apis-in-dot-net-a-practical-guide) situations?

## Replies

### Reply by Pravesh Singh

Hi Ankita,\

I usually use it for strings or nullable types.

```
string s = someString ?? "Default message";is easier than   string s;   if(someString == null)     s = "Default Message";           else     s = someString;or  string s = someString != null ? someString : "Default Message";
```

### Reply by Pravesh Singh

Post is removed by the User.


---

Original Source: https://www.mindstick.com/forum/1917/how-useful-is-c-sharp-s-operator

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
