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

Tagged as

Go to top

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'

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:

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.

License

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

Share

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

 
GeneralThank you bob. PinmemberHiren Solanki28-Dec-10 18:14 

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

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

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