Open In App

C# | Equals(String, String) Method

Improve
Improve
Like Article
Like
Save
Share
Report

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)

Equals(String, String) is always Case-Sensitive. It will return false when there is the same value with the change in Case.

Example:

string str1 = “ABC”;

string str2 = “abc”;

bool string.Equals(str1, str2)

return false.

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:

CSharp




// 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 objects are equal
        Console.WriteLine(s1.Equals(s2));
    }
}


Output: 

True

 

  • Program 2:

CSharp




// 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

 



Last Updated : 19 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads