Char.Equals() Method in C#

The Char.Equals() method in C# is used to compare two character values for equality. It returns true if the character instance is equal to the specified character value, and false otherwise. This method is case-sensitive and performs an exact character comparison.

Syntax

Following is the syntax for the Char.Equals() method −

public bool Equals(char value);
public override bool Equals(object obj);

Parameters

  • value − A character to compare with the current character instance.

  • obj − An object to compare with the current character instance.

Return Value

The method returns a bool value −

  • true if the character values are equal.

  • false if the character values are different.

Using Char.Equals() with Character Comparison

Example

using System;

public class Demo {
   public static void Main() {
      char val = 'A';
      bool res;
      res = val.Equals('A');
      Console.WriteLine("Return Value = " + res);
      
      res = val.Equals('a');
      Console.WriteLine("Return Value (different case) = " + res);
   }
}

The output of the above code is −

Return Value = True
Return Value (different case) = False

Using Char.Equals() with Different Characters

Example

using System;

public class Demo {
   public static void Main() {
      char val = 'H';
      bool res;
      res = val.Equals('d');
      Console.WriteLine("'H' equals 'd': " + res);
      
      res = val.Equals('H');
      Console.WriteLine("'H' equals 'H': " + res);
      
      char digit = '5';
      res = digit.Equals('5');
      Console.WriteLine("'5' equals '5': " + res);
   }
}

The output of the above code is −

'H' equals 'd': False
'H' equals 'H': True
'5' equals '5': True

Using Char.Equals() with Object Parameter

Example

using System;

public class Demo {
   public static void Main() {
      char val = 'X';
      object charObj = 'X';
      object stringObj = "X";
      
      bool res1 = val.Equals(charObj);
      Console.WriteLine("char 'X' equals char object 'X': " + res1);
      
      bool res2 = val.Equals(stringObj);
      Console.WriteLine("char 'X' equals string object "X": " + res2);
   }
}

The output of the above code is −

char 'X' equals char object 'X': True
char 'X' equals string object "X": False

Key Points

  • The Char.Equals() method is case-sensitive − 'A' and 'a' are not equal.

  • When using the object parameter version, the object must be of type char to return true.

  • The method can compare Unicode characters and special characters as well.

  • It performs value comparison, not reference comparison.

Conclusion

The Char.Equals() method provides a reliable way to compare character values in C#. It performs case-sensitive comparison and can work with both character and object parameters, making it useful for character validation and comparison operations in string processing applications.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements