Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Tip/Trick

The ?? Operator

Rate me:
Please Sign up or sign in to vote.
4.50/5 (27 votes)
4 Sep 2012CPOL 79.5K   13   35
This operator is introduced to set value in place of null value, it can also be defined in words like 'In case of null, pick value from another'

Introduction

Operator is introduced with Nullable datatype inclusion in .NET Framework operator ?? can also be referred in words like 'In case of null, pick value from another'.

Scenario

Suppose you're assigning a value to Nullable bool like:

C#
bool? b = null;
At the time of checking value, it will give you an error like:

C#
if(b) //Error CS0266.
{
 
}

So it's always preferable to use ?? to prevent error like:

C#
if(b ?? false)
{
}

It defines that, in case b is null, pick the value false.

?? can also be used in multiple choice of value like:

C#
bool ? a = null
bool ? b = null
bool ? c = true
 
a = b ?? c ?? false;

That will check b first if b is undefined or null, then it will move further to check for c if that also has null then it will set false to a.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on ASP.net,C#,SQL Query,SSRS,Crystal Reports

Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.

He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

Comments and Discussions

 
General@JH64 : Thanks. Yes that's some excessive white space while ... Pin
Hiren solanki27-Dec-10 20:28
Hiren solanki27-Dec-10 20:28 
GeneralHiren, unfortunately, adding "Also, it is not limited to nul... Pin
Sergey Alexandrovich Kryukov27-Dec-10 20:28
mvaSergey Alexandrovich Kryukov27-Dec-10 20:28 
General@brad : I haven't said it's limited to NULL only I said it w... Pin
Hiren solanki27-Dec-10 20:27
Hiren solanki27-Dec-10 20:27 
GeneralReason for my vote of 4 Nice. I will try to use ??. I think... Pin
JH6427-Dec-10 7:16
JH6427-Dec-10 7:16 
GeneralIt is called the null-coalescing (or coalesce) operator. It... Pin
brad.ford@cudl.com27-Dec-10 6:35
brad.ford@cudl.com27-Dec-10 6:35 
General@SledgeHammer01 : It was just reference from MSDN, Not copy ... Pin
Hiren solanki23-Dec-10 18:33
Hiren solanki23-Dec-10 18:33 
SuggestionRe: @SledgeHammer01 : It was just reference from MSDN, Not copy ... Pin
Clifford Nelson7-Sep-12 14:29
Clifford Nelson7-Sep-12 14:29 
GeneralRe: @SledgeHammer01 : It was just reference from MSDN, Not copy ... Pin
Hiren solanki7-Sep-12 18:50
Hiren solanki7-Sep-12 18:50 
GeneralReason for my vote of 5 nice tip Pin
Sandesh M Patil23-Dec-10 5:32
Sandesh M Patil23-Dec-10 5:32 
GeneralReason for my vote of 2 copy and paste from MSDN Pin
SledgeHammer0123-Dec-10 5:11
SledgeHammer0123-Dec-10 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.