Click here to Skip to main content
Click here to Skip to main content

The ?? operator

By , 4 Sep 2012
 

Introduction

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

Scenario

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 using ?? to prevent error like
if(b ?? false)
{
}
It defines that, in case b is null, pick the value false.
?? can be also 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's also having 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)

About the Author

Hiren solanki
Software Developer
India India
Member
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!!!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberafaz4 Sep '12 - 22:52 
Poor content
GeneralRe: My vote of 1 PinmemberHiren solanki4 Sep '12 - 23:00 
Content of your reply is too poor to justify my TIP.
 
I would like to know your suggestion on what additionally I can write on ?? operator, Doesn't this enough to get you understand the whole idea behind ?? operator ? or you want to go beyond the code and want to know the operation takes place at compiler, and assembly level ?
 
BTW it's a TIP not an article.
-Signature is under construction-

GeneralRe: My vote of 1 PinmemberAndreas 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");
Etc.
 
Rule:
 
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.
 
Cheers
Andi
GeneralRe: My vote of 1 PinmemberHiren solanki5 Sep '12 - 3:58 
Thanks Andi for your valuable input.
 
I created this TIP 1.5 year ago in CP. Today After 1.5 year I just updated a single type from 'boll' to 'bool'.
 
I think my typo modification is worth at this time getting good amount of knowledge from you.
 
Thanks.
-Signature is under construction-

GeneralRe: My vote of 1 PinmemberAndreas Gieriet5 Sep '12 - 5:12 
You are welcome.
Cheers
Andi

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

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