Relational operators in C# – Relational operators in C# language e.g. equal, greater than or less than etc. are used to check the relationship between two variables or expressions. It is also known as comparison operators in C# programming.

For example,

If int a =10 and int b =20;
Then, we car write a relation ship as a == b to check if a and b are equal.

Or as , a > = b; to check if a is >= b etc.

NOTE: All Relational operators returns Boolean value. E.g. considering above value of a and b, the expression, a == b, returns false, as a is 10 and b=20.

List of relational operators in C# language.

= = Equal to, e.g. a == b;

> Greater than e.g. a > b;

< Less that e.g. a < b; > = Greater than or equal to. E.g. a >= b;

< = Less than or equal to. E.g. a <=b;

!= Not equal to. E.g. a != b;

C# Code:

Uses of all relational operators are shown by C# program example. All expressions using relational operators returns true or false as an output.

public class Sample
    {

        public static void Main(String[] args) {
 
		int a = 10;
		int b = 20;
 
		// Check if a is Equal to b
		Console.WriteLine("a == b :" + (a == b));
 
		// Check if a is greater than b
		Console.WriteLine("a > b :" + (a > b));
 
		// Check if a is less than b
		Console.WriteLine("a < b :" + (a = b :" + (a >= b));
 
		// Check if a is less than or equal to b
		Console.WriteLine("a <= b :" + (a <= b));
 
		// Check if a is not equal to b
        Console.WriteLine("a != b :" + (a != b));
 
	}
    }

Output:
a == b :false
a > b :false
a = b :false
a <= b :true
a != b :true

Related Posts