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.
Submitted by IncludeHelp, on April 06, 2019 [Last updated : March 16, 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 »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.