DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

C# Null Coalescing Operator

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 11, 10 · · Interview
Like (0)
Save
Tweet
5.93K Views

Join the DZone community and get the full member experience.

Join For Free
One of the coolest and little known features of C# is the null coalescing operator (??), which was added in C# 2.0.  It add some syntactical sugar to help when dealing with possible null values.  Imagine that we have some code that takes input from a user that could be coming in as null and we need to set a default if it is null.  The C# code for this may look something like this:
string val;

if(userinputstring == null)
{
val = "Default Value";
}
else
{
val = userinputstring;
}
This is a very longhand method of doing the check. While it can make it easier to read, you could shorten it a bit by using in inline if:
string val = (userinputstring == null) "Default Value" ? userinputstring;
The inline if is much shorter, but inline ifs can be complex and difficult to read at times.  I don't think the above example necessarily is, but I have seen them where they can be.  Using the null coalescing operator, we can shorten it even more:
string val = userinputstring ?? "Default Value";
This is certainly shorter and doesn't add as much complexity as the inline if.  What the above code is doing is checking to see if the first value is null and it if isn't, it will set the val variable to the value of userinputstring.  If userinputstring is null, then val will be set to "Default Value".  It provides a very easy way to set default values without writing a bunch of plumbing code for each item.  I first saw this concept in SQL Server with the coalesce function and loved it.  Now I get to use in C# too.
Operator (extension)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Role of Development Team in an Agile Environment
  • The Most Popular Kubernetes Alternatives and Competitors
  • 5 Steps to Strengthen API Security
  • Implementing RBAC Configuration for Kubernetes Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo