Information Technology Reference
In-Depth Information
Relational and Equality Comparison Operators
The relational and equality comparison operators are binary operators that compare their
operands and return a value of type
bool
. These operators are listed in Table 8-8.
The relational and equality operators are binary and left-associative.
Table 8-8.
The Relational and Equality Comparison Operators
Operator
Name
Description
<
true
if first operand is less than second operand;
false
otherwise
Less than
>
true
if first operand is greater than second operand;
false
otherwise
Greater than
<=
true
if first operand is less than or equal to second oper-
and;
false
otherwise
Less than or equal to
>=
true
if first operand is greater than or equal to second
operand;
false
otherwise
Greater than or equal to
==
true
if first operand is equal to second operand;
false
otherwise
Equal to
!=
true
if first operand is not equal to second operand;
false
otherwise
Not equal to
A binary expression with a relational or equality operator returns a value of type
bool
.
■
Note
Unlike C and C++, numbers in C# do not have a Boolean interpretation.
int x = 5;
if( x ) // Wrong. x is of type int, not type boolean.
...
if( x == 5 ) // Fine, since expression returns a value of type boolean
...
When printed, the Boolean values
true
and
false
are represented by the string output val-
ues
True
and
False
.
int x = 5, y = 4;
Console.WriteLine("x == x is {0}", x == x);
Console.WriteLine("x == y is {0}", x == y);