find and solve || findandsolve.com
Please wait.....

Relational Operators in C#

In c# programming language, the Relational Operators are used to check the relation between two operands. It is checked if the values of two operands are equal or not, if values are equal then the condition becomes false.

Generally, the c# programming language relational operators will return true only when the defined operands relationship becomes true. Otherwise, it will return false.

For example, you have integer variables firstNumber= 12, secondNumber = 18. If you apply a relational operator >= (firstNumber >= secondNumber), you will get the result false because the variable “firstNumber” contains a value that is less than variable secondNumber.

The given table lists the different types of operators available in c# relational operators.

OperatorNameDescriptionExample (firstNumber = 12, secondNumber = 18)
==Equal tocompares two operands, and it returns true if both are the same.firstNumber==secondNumber (Result is "false")
>Greater than compares whether the left operand greater than the right operand or not and returns true if it is satisfied.firstNumber > secondNumber (Result is "false")
<Less than compares whether the left operand less than the right operand or not and returns true if it is satisfied.firstNumber < secondNumber (Result is "true")
>=Greater than or Equal to compares whether the left operand greater than or equal to the right operand or not and returns true if it is satisfied.firstNumber >= secondNumber (Result is "false")
<=Less than or Equal to compares whether the left operand less than or equal to the right operand or not and returns true if it is satisfied.firstNumber <= secondNumber (Result is "true")
!=Is Not  Equal tochecks whether two operand values equal or not and return true if values are not equal.firstNumber != econdNumber (Result is "true")


C# Relational Operators Example

using System;

namespace FindAndSolve
{
    public class Program
    {
        public static void Main(string[] args)
        {
            bool result;
            int firstNumber = 12, secondNumber = 18;
            result = (firstNumber == secondNumber);
            Console.WriteLine("Equal to Operator: " + result);
            result = (firstNumber > secondNumber);
            Console.WriteLine("Greater than Operator: " + result);
            result = (firstNumber <= secondNumber);
            Console.WriteLine("Lesser than or Equal to: "+ result);
            result = (firstNumber != secondNumber);
            Console.WriteLine("Is Not Equal to Operator: " + result);
        }
    }
}

Output

Equal to Operator: False

Greater than Operator: False

Lesser than or Equal to: True

Is Not Equal to Operator: True