C# Relational Operators with Examples

In c#, Relational Operators are used to check the relation between two operands like we can determine whether two operand values equal or not, etc. based on our requirements.

 

Generally, in c# the relational operators will return true only when the defined operands relationship become true, otherwise it will return false.

 

For example, we have an integer variables a = 10, b = 20 and if we apply a relational operator >= (a >= b), we will get the result false because the variable “a” contains a value which is less than variable b.

 

Following table lists the different type of operators available in c# relational operators.

 

OperatorNameDescriptionExample (a = 6, b = 3)
== Equal to It compare two operands and return true if both are same. a == b (false)
> Greater than It compare whether left operand greater than right operand or not and return true if it satisfied. a > b (true)
< Less than It compare whether left operand less than right operand or not and return true if it satisfied. a < b (false)
>= Greater than or Equal to It compare whether left operand greater than or equal to right operand or not and return true if it satisfied. a >= b (true)
<= Less than or Equal to It compare whether left operand less than or equal to right operand or not and return true if it satisfied. a <= b (false)
!= Not Equal to It checks whether two operand values equal or not and return true if values are not equal. a != b (true)

C# Relational Operators Example

Following is the example of using the Relational Operators in c# programming language.

 

using System;

 

namespace Tutlane

{

   class Program

   {

       static void Main(string[] args)

       {

           bool result;

           int x = 10, y = 20;

           result = (x == y);

           Console.WriteLine("Equal to Operator: " + result);

           result = (x > y);

           Console.WriteLine("Greater than Operator: " + result);

           result = (x <= y);

           Console.WriteLine("Lesser than or Equal to: "+ result);

           result = (x != y);

           Console.WriteLine("Not Equal to Operator: " + result);

           Console.WriteLine("Press Enter Key to Exit..");

           Console.ReadLine();

       }

   }

}

If you observe above code, we used a different Relational operators (<, >, ==) to perform required operations on defined operands.

Output of C# Relational Operators Example

When we execute above c# program, we will get the result like as shown below.

 

C# Relational Operators Example Result

 

This is how we can use the relational operators in c# programming language to check the relationship between defined operands based on our requirements.