C# | Char.Equals() Method

In C#, Char.Equals() is a System.Char struct method which is used to return a value by checking whether current instance is equal to a specified object or Char value. This method can be overloaded by passing different type of arguments to it.

  1. Char.Equals(Char) Method
  2. Char.Equals(Object) Method

Char.Equals(Char) Method

This method is used to returns a value by checking whether the current instance is equal to the specified Char object or not.

Syntax:



public bool Equals(Char ob);

Parameter:

ob: It is the required object which is to be compared with the value of current instance.

Return Type: If the given ob parameter is equal to the value of current instance then it returns true otherwise false. The return type of this method is System.Boolean.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Char.Equals(Char) Method
using System;
  
public class GeeksforGeeks {
  
    // Main method
    public static void Main() {
  
        // declaration of datatype
        bool result;
        char ch1 = 'G';
  
        // checking if 'G' is equal or not
  
        // Here we are passing char G as the
        // parameter to the Equals Method
        result = ch1.Equals('G');
  
        Console.WriteLine(result);    
          
        // checking if 'v' is equal or not
        char ch2 = 'v';
  
        // Here we are passing char W as the
        // parameter to the Equals Method
        result = ch2.Equals('W');
  
        Console.WriteLine(result);        
    }
}

chevron_right


Output:

True
False

Char.Equals(Object) Method

This method is used to returns a value by checking whether the current instance is equal to the specified object or not.

Syntax:

public override bool Equals(object ob);

Parameter:

ob: It is the required object which is to be compared with the current instance or null.

Return Type: If the given ob parameter is an instance of Char and equals to the value of current instance then it returns true otherwise false. The return type of this method is System.Boolean.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Char.Equals(Object) Method
using System;
  
public class GeeksforGeeks {
  
    // Main method
    public static void Main() {
  
        // Declaration of data type
        bool result;
  
        // Checking if 'G' is equal or not
        char ch1 = 'G';
  
        // Here we are passing object ch1 as the
        // parameter to the Equals Method
        result = 'G'.Equals(ch1);
  
        Console.WriteLine(result);  
   
        // Checking if 'v' is equal or not
        char ch2 = 'v';
  
         // Here we are passing object ch2 as the
        // parameter to the Equals Method
        result = 'x'.Equals(ch2);
  
        Console.WriteLine(result);
    }
}

chevron_right


Output:

True
False

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.char.equals?view=netframework-4.7.2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.