Chaining comparison operators in C#

CsharpProgrammingServer Side Programming

C# has many operators that work on the left-right and righ-left associativity.

Chanining depends on the left-to-right associativity on operators with same precedence.

Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. Certain operators have higher precedence than others do; for example, the multiplication operator has higher precedence than the addition operator.

The operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators are evaluated first.

To check whether a string is null or not, you can write it as −

if (str == null == false)


The associative of equality operators like == and != is from left to right. Therefore, above it will automatically check for the following first −

str==null

Published on 17-Sep-2018 12:42:36