C# | Equals(String, String) Method

In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false. This method is different from Compare and CompareTo methods. This method compares two string on the basis of contents.

Syntax :

bool string.Equals(string str1, string str2)

Explanation: This method will take the two parameters in the form of string object and check them for equality. After checking, this method will return Boolean values. The return value type of this method is System.Boolean This method will return true if the value of str1 is the same as the value of str2 otherwise, false. If both str1 and str2 are null, then the method will return true.

Example :

Input:          string str1 = "ProGeek 2.0";
                string str2 = "ProGeek 2.0";
                string.Equals(str1, str2)
Output:         True

Input:          string str3 = "GFG";
                string str4 = "others";
                string.Equals(str3, str4)
Output:         False

Below are the programs to demonstrate the above method :

  • Program 1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the Equals() Method
    using System;
      
    class GFG {
      
        // Main Method
        public static void Main(string[] args)
        {
            string s1 = "ProGeek 2.0";
            string s2 = "ProGeek 2.0";
      
            // Equals() method return true
            // as both string objetcs are equal
            Console.WriteLine(s1.Equals(s2));
        }
    }

    chevron_right

    
    

    Output:

    True
    
  • Program 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the Equals() Method
    using System;
    class Geeks {
      
        // Main Method
        public static void Main(string[] args)
        {
            string s1 = "GFG";
            string s2 = "others";
          
            // this will give result false as 
            // both s1 and s2 are different
            Console.WriteLine(s1.Equals(s2));
        }
    }

    chevron_right

    
    

    Output:

    False
    


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.