C# Relational Operators
The eleventh part of the
C# Fundamentals tutorial returns to the generic
operators by describing the relational operators. These useful
operators permit the developer to compare two values and make
decisions based upon the result of the comparison.
What Can Be Compared?
The relational operators provide the programmer with the
means to compare two values. The developer can determine if the
two values are equal or not and can determine which of the two
values is larger or smaller. All of the relational operations
return a Boolean result accordingly.
In the case of the simple 'equal to' and 'not equal to'
operations, any two compatible data types can be compared.
However, for the comparison operators that determine if
one value is greater than another, the data type must support
the concept of ordering. This includes all of the basic numeric
types but not Boolean values as it is not sensible to consider
'true' to be either greater than or less than 'false'.
Equal To Operator
The equal to operator compares two values of any
data type and returns a 'true' result if the two operands are
identical. The operator symbol for equal to is a doubleequals
signs (==).
int a = 55;
int b = 65;
int c = 65;
bool result;
result = a == b;
result = b == c;
Not Equal To Operator
The not equal to operator compares two operands and
returns true if the two values do not match exactly. The
operator provides the opposite functionality to the equal to
operator mentioned above. The operator symbol for not
equal to is an exclamation mark and an equals signs (!=).
int a = 55;
int b = 65;
int c = 65;
bool result;
result = a != b;
result = b != c;
Comparison Operators
The remainder of the relational operators are also known as
the comparison operators. These provide the software
developer with the means to compare two values to determine
which is the greater. These operators are available only to
data types that support the concept of ordering.
Four comparison operators are available in the C# programming
language. These are greater than (>), less than
(<), greater than or equal to (>=) and less than or
equal to (<=). In each case, the operand to the left of
the operator is being compared; the statement a>b
therefore asks the question, is 'a' greater than 'b'?
The following examples show the use of the four comparison
operators.
int a = 55;
int b = 65;
int c = 65;
bool result;
result = a > b;
result = a < b;
result = a >= b;
result = a <= b;
result = b > c;
result = b < c;
result = b >= c;
result = b <= c;
Conditional Processing
The relational operators provide methods for comparing two
values. In the examples provided above, the concepts should be
clear but the productive use of the operators is not as
apparent. The general use of these operators is to test a
condition and change the flow of the program or the value of a
variable accordingly. This is known as conditional
processing and will be explained in detail later in the C#
Fundamentals tutorial.
Operator Precedence
We can now extend the operator precedence table further using
the new operators discussed in this article.
Parentheses Operator 
() 
Increment / Decrement Operators 
++(postfix) (postfix) 
++(prefix) (prefix) 
Complement Operators 
! ~ 
Basic Arithmetic Operators 
* / % +  
Bitwise Shift Operators 
<< >> 
Comparison Operators 
< > <= >= 
Equivalence Operators 
== != 
Logic / Bitwise Operators 
& ^  &&  
Assignment Operator 
= 
Arithmetic Assignment Operators 
*= /= %= += =
>>= <<= &= ^= = 
