Click here to Skip to main content
14,487,386 members

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

Rate this:
4.88 (12 votes)
Please Sign up or sign in to vote.
4.88 (12 votes)
4 Feb 2014CPOL
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 = nullint bar = foo ?? 7;
// bar == 7
It is a shorthand for:
int? foo = nullint 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 30 years of experience in information systems and software development for the industry. He is Senior Controls Engineer at OTTO Motors

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.

Comments and Discussions

 
GeneralThanks!!! Pin
chuckamus_prime11-Feb-14 4:28
Memberchuckamus_prime11-Feb-14 4:28 
GeneralMy vote of 4 Pin
Ramandeep Singh Patial4-Feb-14 22:44
MemberRamandeep Singh Patial4-Feb-14 22:44 
QuestionMy golden rule Pin
Simon Gulliver4-Feb-14 22:24
professionalSimon Gulliver4-Feb-14 22:24 
GeneralMy vote of 5 Pin
Klaus Luedenscheidt4-Feb-14 18:50
MemberKlaus Luedenscheidt4-Feb-14 18:50 
GeneralRe: My vote of 5 Pin
dojohansen4-Feb-14 20:24
Memberdojohansen4-Feb-14 20:24 
QuestionNot much different than operator ?= Pin
Philippe Mori4-Feb-14 14:03
MemberPhilippe Mori4-Feb-14 14: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.

Tip/Trick
Posted 4 Feb 2014

Tagged as

Stats

13K views
4 bookmarked