Click here to Skip to main content
12,622,568 members (33,331 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.1K views
4 bookmarked
Posted

Beware: The null-coalescing (??) operator is low in the order of operator precedence.

, 4 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Maybe writing a tip will mean the last time I fall for this.

Introduction

One of my favourite operators is the null-coalescing, or ?? operator.  It is used with a nullable type to evaluate to a non-null value when the nullable value is null.  For example: 

Background

int? foo = null; 
int bar = foo ?? 7;
// bar == 7
It is a shorthand for:
int? foo = null; 
int bar = foo == null ? 7 : foo;
// bar == 7

Order of Operator Precedence

It is important, however, to keep in mind the order of operator precedence:

int? top = 60;
int? bottom = 180;
int height = bottom ?? 0 - top ?? 0;

// So height == 120?
Nope. When you look at this code, it seems like it should be evaluated like:
int? top = 60;
int? bottom = 180;
int height = (bottom ?? 0) - (top ?? 0);

// height == 120
However, it is actually evaluated:
int? top = 60;
int? bottom = 180;
int height = bottom ?? (0 - top ?? 0);

// height == 180

This is caused me wasted debugging time on more than one occasion.

License

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

Share

About the Author

Yvan Rodrigues
Systems Engineer Clearpath Robotics
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the industry. He is Senior Systems Developer at Clearpath Robotics

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.

Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

You may also be interested in...

Comments and Discussions

 
GeneralThanks!!! Pin
chuckamus_prime11-Feb-14 5:28
memberchuckamus_prime11-Feb-14 5:28 
GeneralMy vote of 4 Pin
Ramansingh4-Feb-14 23:44
professionalRamansingh4-Feb-14 23:44 
QuestionMy golden rule Pin
Simon Gulliver4-Feb-14 23:24
memberSimon Gulliver4-Feb-14 23:24 
GeneralMy vote of 5 Pin
Klaus Luedenscheidt4-Feb-14 19:50
memberKlaus Luedenscheidt4-Feb-14 19:50 
GeneralRe: My vote of 5 Pin
dojohansen4-Feb-14 21:24
memberdojohansen4-Feb-14 21:24 
QuestionNot much different than operator ?= Pin
Philippe Mori4-Feb-14 15:03
memberPhilippe Mori4-Feb-14 15:03 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 4 Feb 2014
Article Copyright 2014 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid