Click here to Skip to main content
13,800,136 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 23 Dec 2010
Licenced CPOL

The ?? Operator

, 4 Sep 2012
Rate this:
Please Sign up or sign in to vote.
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'


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'.


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

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

if(b) //Error CS0266.

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

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:

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.


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


About the Author

Hiren solanki
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on,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!!!

You may also be interested in...

Comments and Discussions

GeneralAre you gonna now describe other operators as well? :) Pin
jszczur3-Jan-11 12:57
memberjszczur3-Jan-11 12:57 
GeneralThank you bob. Pin
Hiren solanki28-Dec-10 19:14
memberHiren solanki28-Dec-10 19:14 
GeneralReason for my vote of 5 Very usefull in database data Pin
Bob Baeck28-Dec-10 10:09
professionalBob Baeck28-Dec-10 10:09 
General@SAKryukov : I am sorry,I didn't get what you're trying to c... Pin
Hiren solanki27-Dec-10 21:30
memberHiren solanki27-Dec-10 21:30 
General@JH64 : Thanks. Yes that's some excessive white space while ... Pin
Hiren solanki27-Dec-10 21:28
memberHiren solanki27-Dec-10 21:28 
GeneralHiren, unfortunately, adding "Also, it is not limited to nul... Pin
Sergey Alexandrovich Kryukov27-Dec-10 21:28
mvpSergey Alexandrovich Kryukov27-Dec-10 21:28 
General@brad : I haven't said it's limited to NULL only I said it w... Pin
Hiren solanki27-Dec-10 21:27
memberHiren solanki27-Dec-10 21:27 
GeneralReason for my vote of 4 Nice. I will try to use ??. I think... Pin
JH6427-Dec-10 8:16
memberJH6427-Dec-10 8:16 
Reason for my vote of 4
Nice. I will try to use ??.

I think this is a typo "boll ? c = true" and what you did mean is "bool ? c = true".
GeneralIt is called the null-coalescing (or coalesce) operator. It... Pin 7:35 7:35 
General@SledgeHammer01 : It was just reference from MSDN, Not copy ... Pin
Hiren solanki23-Dec-10 19:33
memberHiren solanki23-Dec-10 19:33 
SuggestionRe: @SledgeHammer01 : It was just reference from MSDN, Not copy ... Pin
Clifford Nelson7-Sep-12 15:29
mvpClifford Nelson7-Sep-12 15:29 
GeneralRe: @SledgeHammer01 : It was just reference from MSDN, Not copy ... Pin
Hiren solanki7-Sep-12 19:50
memberHiren solanki7-Sep-12 19:50 
GeneralReason for my vote of 5 nice tip Pin
Sandesh M Patil23-Dec-10 6:32
memberSandesh M Patil23-Dec-10 6:32 
GeneralReason for my vote of 2 copy and paste from MSDN Pin
SledgeHammer0123-Dec-10 6:11
memberSledgeHammer0123-Dec-10 6: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181214.1 | Last Updated 5 Sep 2012
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid