Relational operators (<, <=, >=, >) on nullable value - CSharp Language Basics

CSharp examples for Language Basics:Nullable

Introduction

comparing a null value to either a null or a non-null value returns false:

int? x = 5;
int? y = null;

bool b = x < y;    // Translation:

bool b = (x.HasValue && y.HasValue)
         ? (x.Value < y.Value)
         : false;

// b is false (assuming x is 5 and y is null)

All other operators (+, -, *, /, %, &, |, ^, <<, >>, +, ++, --, !, ~)

These operators return null when any of the operands are null.


Related Tutorials