C# Equal To and Not Equal To Operators Example

C# example for equal to (==) and not equal to (!=) operators: Here, we are writing a C# program to demonstrate example of equal to and not equal to operators. By IncludeHelp Last updated : April 15, 2023

Equal To Operator

Equal To (==) and Not Equal To (!=) operators are used for comparison, they are used to compare two operands and return Boolean value.

Equal To (==) operator returns True – if both operand's values are equal, else it returns False.

Not Equal To Operator

Not Equal To (!=) operator returns True – both operand's values are not equal, else it returns False.

Syntax

Operand1 == Operand2
Operand1 != Operand2

Example

Input:
int a = 10;
int b = 3;
    
Console.WriteLine("a==b: {0}", (a == b));
Console.WriteLine("a!=b: {0}", (a != b));

Output:
a==b: False
a!=b: True

C# code to demonstrate example of Equal To and Not Equal To operators

// C# program to demonstrate example of 
// equal to and not equal to operators

using System;
using System.IO;
using System.Text;

namespace IncludeHelp {
  class Test {
    // Main Method 
    static void Main(string[] args) {

      int a = 10;
      int b = 3;

      //printing return type
      Console.WriteLine("Return type of == operator: {0}", (a == b).GetType());
      Console.WriteLine("Return type of != operator: {0}", (a != b).GetType());

      //printing return values
      Console.WriteLine("a==b: {0}", (a == b));
      Console.WriteLine("a!=b: {0}", (a != b));

      //checking conditions
      if (a == b)
        Console.WriteLine("both are equal");
      else
        Console.WriteLine("both are not equal");

      //checking conditions in another way
      if ((a == b) == true)
        Console.WriteLine("both are equal");
      else
        Console.WriteLine("both are not equal");

      //hit ENTER to exit the program
      Console.ReadLine();
    }
  }
}

Output

Return type of == operator: System.Boolean
Return type of != operator: System.Boolean
a==b: False
a!=b: True
both are not equal
both are not equal

C# Basic Programs »


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.