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;
It is a shorthand for:
int? foo = null;
int bar = foo == null ? 7 : foo;
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;
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);
However, it is actually evaluated:
int? top = 60;
int? bottom = 180;
int height = bottom ?? (0 - top ?? 0);
This is caused me wasted debugging time on more than one occasion.
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.