Click here to Skip to main content
15,886,030 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
4 Feb 2014CPOL 19.4K   4   6
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

C#
int? foo = nullint bar = foo ?? 7;
// bar == 7
It is a shorthand for:
C#
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:

C#
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:
C#
int? top = 60;
int? bottom = 180;
int height = (bottom ?? 0) - (top ?? 0);

// height == 120
However, it is actually evaluated:
C#
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)


Written By
Engineer Robotic Assistance Devices / AITX
Canada Canada
Yvan Rodrigues has 30 years of experience in information systems and software development for the industry. He is Senior Concept Designer at Robotic Assistance Devices

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, the University of Waterloo as Information Systems Manager, and OTTO Motors as Senior Systems Engineer and Senior Concept Designer.

Yvan is currently focused on design of embedded systems.

Comments and Discussions

 
GeneralThanks!!! Pin
chuckamus_prime11-Feb-14 4:28
chuckamus_prime11-Feb-14 4:28 
GeneralMy vote of 4 Pin
Ramandeep Singh Patial4-Feb-14 22:44
Ramandeep Singh Patial4-Feb-14 22:44 
QuestionMy golden rule Pin
Simon Gulliver4-Feb-14 22:24
professionalSimon Gulliver4-Feb-14 22:24 
Always include parentheses in complicated expressions (even if you don't need them). Woops, in this case you did.
GeneralMy vote of 5 Pin
Klaus Luedenscheidt4-Feb-14 18:50
Klaus Luedenscheidt4-Feb-14 18:50 
GeneralRe: My vote of 5 Pin
dojohansen4-Feb-14 20:24
dojohansen4-Feb-14 20:24 
QuestionNot much different than operator ?= Pin
Philippe Mori4-Feb-14 14:03
Philippe 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.