Loading...

- Access to all 42 C# articles plus all upcoming, new content.
- Save countless hours of confusion and frustration with high-quality, easy-to-follow programming tutorials.
- Unrestricted access to 317 MultiCharts, TradingView, and MultiCharts .NET articles.

Subscriptions start at just $15 per month.

Join today and take your trading code programming to the next level!

When programming in C# we often want to know how one value compares to another. How can we decide whether a certain value is bigger, smaller, or equal to another value?

## C#’s relational operators: comparing values for true or false

**Relational operators** compare two values and return a Boolean `true`

or `false`

value depending on the result (Dorman, 2010; Liberty & MacDonald, 2009; Stephens, 2014). Other names for these operators are *conditional operators* (Stellman & Greene, 2010) and *comparison operators* (Hejlsberg, Torgersen, Wiltamuth, & Golde, 2011; Stephens, 2014).

An example of a relational operator is the *less than* operator (`<`

). This operator returns true when the value on its left is less than the value on its right. So, `3 < 10`

returns true, whereas `9 < 1`

returns false.

Relational operators are often used with if statements. For example:

int x = 10; int y = 5; if (x > y) Console.WriteLine("x is greater than y"); if (x <= y) Console.WriteLine("x is less than or equal to y"); //> x is greater than y

Here two integers are declared, `x`

and `y`

, which are assigned a value of 10 and 5, respectively. The first if statement then evaluates whether `x`

is *greater than* (`>`

) `y`

. Since that expression returns true, the message is printed to the console with `Console.WriteLine()`

.

The second if statement uses the *less than or equal to* relational operator (`<=`

) to compare the value of `x`

against the value of `y`

. Since `x`

is greater than `y`

(and not less than or equal to), this expression returns false and the if statement’s code isn’t executed.

## Assigning the result of relational operators to a Boolean variable

Since relational operators return a Boolean value (Dorman, 2010), this value can be stored in `bool`

variables as follows:

int m = 42; int n = 27; bool myBoolA = (m > n); bool myBoolB = (m <= n); Console.WriteLine("myBoolA = {0}, myBoolB = {1}", myBoolA, myBoolB); //> myBoolA = True, myBoolB = False

Two integer variables (`m`

and `n`

) are declared here and assigned values of 42 and 27. Then a pair of Boolean variables are created, which have their values assigned based on comparing the `m`

and `n`

variables against each other. These expressions are placed in optional parentheses to make the code clearer.

## An overview of all relational operators in C#

The table below lists all relational operators (Liberty & MacDonald, 2009; Dorman, 2010):

Name and meaning | Operator | Expression | Evaluates to (when `x` is 10) |
---|---|---|---|

Equals | `==` |
`x == 12` |
False |

`x == 10` |
True; `x` is only equal to 10 and unequal to everything else |
||

Not equals | `!=` |
`x != 10` |
False |

`x != 9` |
True; `x` is unequal to everything except 10 |
||

Greater than | `>` |
`x > 3` |
True |

`x > 10` |
False; `x` is 10 and not greater than 10 |
||

Greater than or equal to |
`>=` |
`x >= 0` |
True; `x` is greater than 0 |

`x >= 10` |
True; `x` is equal to 10 |
||

`x >= 99` |
False | ||

Less than | `<` |
`x < 9` |
False |

`x < 50` |
True; `x` is less than 50. |
||

Less than or equal to |
`<=` |
`x <= 9` |
False |

`x <= 10` |
True; `x` is equal to 10 |
||

`x <= 25` |
True; `x` is less than 25. |

`==`

) while the assignment operator (`=`

) has one. A mistake that programmers make from time to time is confusing these two (Liberty & MacDonald, 2009).
## Using relational operators with characters and strings

The relational operators work for all numeric types in the manner displayed in the table above. Interestingly, they also work with `char`

types (Sempf, Sphar, & Davis, 2010):

bool myBool = 'a' > 'b'; Console.WriteLine("a is greater than b? {0}", myBool); //> a is greater than b? False myBool = 'A' < 'a'; Console.WriteLine("Capital A is less than lower a? {0}", myBool); //> Capital A is less than lower a? True myBool = 'A' < 'b'; Console.WriteLine("Capital A is less than lower b? {0}", myBool); //> Capital A is less than lower b? True

The expression `a > b`

returns false because, alphabetically, ‘b’ is greater than ‘a’. And the other two comparisons, `A < a`

and `A < b`

, both return true because all upper letters are less than lower letters (Sempf et al., 2010). By the way, when comparing strings only the equals operator (`==`

) can be used (Sempf et al., 2010).

## More about relational operators in C#

Beside the relational operators there are also logical operators in C#. The difference between both is that relational operators compare values, whereas logical operators evaluate Boolean expressions (Dorman, 2010).

Relational operators should be used with caution with real numbers (Albahari & Albahari, 2012). For more on this, see comparing floating point numbers.

### Summary

Relational operators compare two values and return `true`

or `false`

depending on the result. These operators are equals (`==`

), not equals (`!=`

), greater than (`>`

), greater than or equal to (`>=`

), less than (`<`

), and less than or equal to (`<=`

).

**References**

Albahari, J. & Albahari, B. (2012). *C# 5.0 in a Nutshell: The Definitive Reference* (5th edition). Sebastopol, CA: O’Reilly Media.

Dorman, S. (2010). *Sams Teach Yourself Visual C# 2010 in 24 Hours*. Indianapolis, IN (USA): Sams/Pearson Education.

Hejlsberg, A., Torgersen, M., Wiltamuth, S., & Golde, P. (2011). *The C# Programming Language: Special Annotated Edition for C# 4.0* (4th edition). Upper Saddle River, NJ: Addison-Wesley.

Liberty, J. & MacDonald, B. (2009). *Learning C# 3.0: Master the Fundamentals of C# 3.0*. Sebastopol, CA: O’Reilly Media.

Sempf, B., Sphar, C., & Davis, S.R. (2010). *C# 2010 All-In-One for Dummies*. Hoboken, NJ: John Wiley & Sons.

Stellman, A. & Greene, J. (2010). *Head First C#: A Brain-Friendly Guide* (2nd edition). Sebastopol, CA: O’Reilly Media.

Stephens, R. (2014). *C# 5.0 Programmer Reference*. Indianapolis, IN: John Wiley & Sons.