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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionSome revisions?memberYusuf Uzun6 Sep '12 - 12:30 
GeneralRe: Some revisions?memberHiren solanki6 Sep '12 - 20:33 
GeneralMy vote of 5memberWolfieH6 Sep '12 - 11:19 
GeneralRe: My vote of 5memberHiren solanki6 Sep '12 - 20:34 
GeneralMy vote of 1 [modified]memberSavalia Manoj M5 Sep '12 - 20:49 
GeneralRe: My vote of 1memberHiren solanki5 Sep '12 - 21:36 
GeneralRe: My vote of 1memberSavalia Manoj M6 Sep '12 - 1:44 
GeneralRe: My vote of 1memberHiren solanki6 Sep '12 - 2:56 
Hi Manoj,
 
You are casting from nullable type to non-nullable type.
 
Either way you aren't using ?? operator. and casted a blind 1 vote on my TIP.
 
Anyway, Here comes my TIP useful to you, Try with this.
 
   int? oval;
   oval = 10;
   int val = oval ?? 0;
-Signature is under construction-

GeneralRe: My vote of 1memberSavalia Manoj M6 Sep '12 - 20:17 
GeneralRe: My vote of 1memberHiren solanki6 Sep '12 - 20:29 
GeneralRe: My vote of 1memberSavalia Manoj M6 Sep '12 - 20:58 
GeneralMy vote of 1memberafaz4 Sep '12 - 22:52 
GeneralRe: My vote of 1memberHiren solanki4 Sep '12 - 23:00 
GeneralRe: My vote of 1memberAndreas Gieriet5 Sep '12 - 3:52 
GeneralRe: My vote of 1memberHiren solanki5 Sep '12 - 3:58 
GeneralRe: My vote of 1memberAndreas Gieriet5 Sep '12 - 5:12 
GeneralMy vote of 4memberAndreas Gieriet18 Apr '12 - 23:18 
GeneralReason for my vote of 5 Good to remind people about (new) la...memberjohannesnestler21 Feb '12 - 3:39 
GeneralReason for my vote of 5 nice onemembernikhi _singh9 Feb '12 - 1:45 
GeneralReason for my vote of 5 Simple tip; Doesn't matter if I coul...memberJustin Helsley6 Oct '11 - 9:23 
GeneralReason for my vote of 5 Completly forgot about this operator...membersucram16 Jan '11 - 22:45 
GeneralAre you gonna now describe other operators as well? :)memberjszczur3 Jan '11 - 11:57 
GeneralThank you bob.memberHiren Solanki28 Dec '10 - 18:14 
GeneralReason for my vote of 5 Very usefull in database datamemberBob Baeck28 Dec '10 - 9:09 
General@SAKryukov : I am sorry,I didn't get what you're trying to c...memberHiren Solanki27 Dec '10 - 20:30 

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

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