Javatpoint Logo
Javatpoint Logo

Char.Equals () Method in C#

Char.Equals() method is used to compare two characters for equality. It is a static method to check whether the two characters given are the same or not. This method returns a Boolean value indicating whether the specified characters are equal.

Syntax:

The syntax for the Char.Equals () method.

The arguments in the method are 'c1' is the first character to compare, and 'c2' is the second character to compare.

The return type of the given method is Boolean, and it returns true if the given characters are equal or the same. Otherwise, it returns false.

Some situations where the Char.Equals() method is used:

Character Equality:

The purpose of this method is to compare two character and check whether the two characters are equal. It is used to perform character-level comparisons.

Example:

Let us take a program to demonstrate Character Equality using Equals() method in C#.

Output:

Char.Equals () Method in C#

Explanation:

The variables 'char1' and 'char2' are declared and initialized to characters 'A' and 'B' respectively. After that, the 'isEqual' variable is of the Boolean type used to store the result of the method Char.Equals(). Char.Equals () method takes char1 and char2 as the arguments and returns true or false. Here, it returns false because both the characters are different. After that, the result is printed.

Case-Sensitive and Case-nsensitive Comparisons:

This method is useful for both case-sensitive and case-insensitive comparisons. In case-insensitive, both capital and small letters are treated as equal. Whereas in case-sensitive the capital and small letters are treated as different.

Example:

The C# program to illustrate the Case-Sensitive and Case-insensitive Comparisons using Char.Equals() method.

Output:

Char.Equals () Method in C#

Explanation:

The variables 'char1' and 'char2' are declared and initialized to characters 'c' and 'C'. After that, the 'isEqual' variable is of the Boolean type used to store the result of the method Char.Equals(). Char.Equals() method takes char1 and char2 as the arguments and returns true or false. Here, it returns false because both the characters are the same, but one is capital, and the other is small. The variable isEqualIgnoreCase is also of Boolean type, but this time both the characters are made to lower case by using the Char.ToLower () method. After that, check whether both the characters are the same or not. Here, the Equals() function will give true as a result. After that, the result is printed to the console.

Unicode Character Comparison:

Characters in C# are Unicode characters, and the Char.Equals() method takes Unicode values into account when performing the comparison.

Example:

Let us take a C# program to perform Unicode Character comparisons using Char.Equals() method in C#

Output:

Char.Equals () Method in C#

Explanation:

In this C# program, three variables are defined: unicodeChar1, representing the Unicode character '\u00E9' (the character 'é'), unicodeChar2, representing the character 'é' written directly, and char3, representing the character 'a'. The program utilizes the Char.Equals() method in two comparisons. In the first comparison, Char.Equals() are employed to compare unicodeChar1 and unicodeChar2 for equality. The resulting variable isEqualUnicode1, which is set to true, signifying that both characters represent the same Unicode character 'é'. In the second comparison, Char.Equals() is used to compare unicodeChar1 and char3. The resulting variable isEqualUnicode2, which is set to false, indicating inequality between the Unicode character 'é' and the character 'a'.

String.Equals() Method:

Char.Equals() method is used to compare characters likewise to compare the strings also Equals() method is used to compare the two Strings. An alternative to using the '==' operator is the use of String.Equals () method. This method also takes two strings as parameters and an optional parameter, 'StringComparison.OrdinalIgnoreCase' to ensure a case-insensitive comparison that treats the two strings as equal even if they are a combination of capital and small letters.

Example:

Let us take a simple C# program to demonstrate the use of the String.Equals () method.

Output:

Char.Equals () Method in C#

Explanation:

The C# program compares the strings "Hello" and "hello" using String.Equals(). The first comparison (isEqualString1) is case-sensitive, producing false, while the second (isEqualString2) is case-insensitive, resulting in true. The program prints these results to the console.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news