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

Tagged as

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
President Red Cell Innovation Inc.
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the manufacturing sector. He runs Red Cell Innovation Inc./L'innovation de Globules Rouges, a consulting company focusing on efficiency and automation of manufacturing and business processes for small businesses, healthcare, and the public sector. He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).
 
Yvan draws on experience at 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.
 
Yvan's consumer-focused apps can be found in the Windows Store, Apple App Store, and Google Play marketplace.
Follow on   Google+   LinkedIn

Comments and Discussions

 
GeneralThanks!!! Pinmemberchuckamus_prime11-Feb-14 5:28 
GeneralMy vote of 4 PinprofessionalRamansingh4-Feb-14 23:44 
QuestionMy golden rule PinmemberSimon Gulliver4-Feb-14 23:24 
GeneralMy vote of 5 PinmemberKlaus Luedenscheidt4-Feb-14 19:50 
GeneralRe: My vote of 5 Pinmemberdojohansen4-Feb-14 21:24 
QuestionNot much different than operator ?= PinmemberPhilippe Mori4-Feb-14 15:03 

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 | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 4 Feb 2014
Article Copyright 2014 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid