Click here to Skip to main content
14,032,887 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

SuggestionSome revisions? Pin
Yusuf Uzun6-Sep-12 12:30
memberYusuf Uzun6-Sep-12 12:30 
GeneralRe: Some revisions? Pin
Hiren solanki6-Sep-12 20:33
memberHiren solanki6-Sep-12 20:33 
GeneralMy vote of 5 Pin
WolfieH6-Sep-12 11:19
memberWolfieH6-Sep-12 11:19 
GeneralRe: My vote of 5 Pin
Hiren solanki6-Sep-12 20:34
memberHiren solanki6-Sep-12 20:34 
GeneralMy vote of 1 Pin
Savalia Manoj M5-Sep-12 20:49
memberSavalia Manoj M5-Sep-12 20:49 
GeneralRe: My vote of 1 Pin
Hiren solanki5-Sep-12 21:36
memberHiren solanki5-Sep-12 21:36 
GeneralRe: My vote of 1 Pin
Savalia Manoj M6-Sep-12 1:44
memberSavalia Manoj M6-Sep-12 1:44 
GeneralRe: My vote of 1 Pin
Hiren solanki6-Sep-12 2:56
memberHiren solanki6-Sep-12 2:56 
GeneralRe: My vote of 1 Pin
Savalia Manoj M6-Sep-12 20:17
memberSavalia Manoj M6-Sep-12 20:17 
GeneralRe: My vote of 1 Pin
Hiren solanki6-Sep-12 20:29
memberHiren solanki6-Sep-12 20:29 
GeneralRe: My vote of 1 Pin
Savalia Manoj M6-Sep-12 20:58
memberSavalia Manoj M6-Sep-12 20:58 
GeneralMy vote of 1 Pin
Abolfazl Khusniddinov4-Sep-12 22:52
memberAbolfazl Khusniddinov4-Sep-12 22:52 
GeneralRe: My vote of 1 Pin
Hiren solanki4-Sep-12 23:00
memberHiren solanki4-Sep-12 23:00 
GeneralRe: My vote of 1 Pin
Andreas Gieriet5-Sep-12 3:52
professionalAndreas Gieriet5-Sep-12 3:52 
The vote of 1 is a bit too low, but I somehow understand why a lower vote is given.

You not even give the proper name to that operator: it's the Null Coalescing (Wikipedia)[^] operator.

IMHO, the given example is kind of poorly chosen: nullable value types are useful for database type map - otherwise I consider them as a code smell.

A more realistic scenario is in connection with reference types and especially with Enumerable<T> (e.g. linq queries):

Record GetFirstOrSentryRecord(Func<MyRecord, bool> predicate)
    var query = from record in table where record=>predicate(record) select record;
    return query.FirstOrDefault() ?? MyRecord.SomeSentryRecord;

Or with defensive programming, at locations where you want no null values, e.g.:
HandleError(..., context ?? "no context");



The ?? operator is everywhere usable where you have an instance of a reference type (including nullable value types) that may evaluate to null.

expandedwith ternary operatorwith null coalescing operator
result = item;
if (result == null)
    result = other;

result = item != null
       ? item
       : other;

result = item
         ?? other;

Finally, a good tip provides references to say where to go from here, e.g.

GeneralRe: My vote of 1 Pin
Hiren solanki5-Sep-12 3:58
memberHiren solanki5-Sep-12 3:58 
GeneralRe: My vote of 1 Pin
Andreas Gieriet5-Sep-12 5:12
professionalAndreas Gieriet5-Sep-12 5:12 
GeneralMy vote of 4 Pin
Andreas Gieriet18-Apr-12 23:18
professionalAndreas Gieriet18-Apr-12 23:18 
GeneralReason for my vote of 5 Good to remind people about (new) la... Pin
johannesnestler21-Feb-12 3:39
memberjohannesnestler21-Feb-12 3:39 
GeneralReason for my vote of 5 nice one Pin
Nikhil_S9-Feb-12 1:45
professionalNikhil_S9-Feb-12 1:45 
GeneralReason for my vote of 5 Simple tip; Doesn't matter if I coul... Pin
Justin Helsley6-Oct-11 9:23
memberJustin Helsley6-Oct-11 9:23 
GeneralReason for my vote of 5 Completly forgot about this operator... Pin
sucram16-Jan-11 22:45
membersucram16-Jan-11 22:45 
GeneralAre you gonna now describe other operators as well? :) Pin
jszczur3-Jan-11 11:57
memberjszczur3-Jan-11 11:57 
GeneralThank you bob. Pin
Hiren solanki28-Dec-10 18:14
memberHiren solanki28-Dec-10 18:14 
GeneralReason for my vote of 5 Very usefull in database data Pin
Bob Baeck28-Dec-10 9:09
professionalBob Baeck28-Dec-10 9:09 
General@SAKryukov : I am sorry,I didn't get what you're trying to c... Pin
Hiren solanki27-Dec-10 20:30
memberHiren solanki27-Dec-10 20:30 

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
Web01 | 2.8.190423.1 | Last Updated 5 Sep 2012
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid