Skip to content
Related Articles

Related Articles

Improve Article
C# | Equals(String, String) Method
  • Last Updated : 31 Jan, 2019

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:




    // 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));
        }
    }
    Output:
    True
    
  • Program 2:




    // 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));
        }
    }
    Output:
    False
    



My Personal Notes arrow_drop_up
Recommended Articles
Page :