C# | Check if two String objects have the same value | Set-1 - GeeksforGeeks

03 Aug, 2021
String.Equals Method method is used to check whether the two String objects have the same value. This method can be overloaded by passing different numbers and types of parameters to it. There are total 5 methods in the overload list of this method in which the first 2 are discussed in this article and the remaining are discussed in Set-2 and Set-3. 
Equals(Object)Equals(String)Equals(String, String)Equals(String, StringComparison)Equals(String, String, StringComparison)
Equals(Object)
Equals(String)
Equals(String, String)
Equals(String, StringComparison)
Equals(String, String, StringComparison)
This method is used to check whether this instance and a specified object, which must also be a String object, have the same value or not. This method also performs an ordinal comparison in both case-sensitive and culture-insensitive.
Syntax: 
public override bool Equals (object ob1);
Here, ob1 is the string object that is used to compare with this instance.
Return Value: The return type of this method is System.Boolean. If ob1 is a String and its value is the same as this instance, then this method will return true, otherwise return false. And if the value of ob1 is null then this method will return false. 
Example:
C#
// C# program to illustrate// Equals(Object) methodusing System; // Structurepublic struct Student{     private string name;     public string StudentName    {        get        {            return name;        }    }     public Student(string Sname)    {        name = Sname;    }     public override string ToString()    {        return name;    }} // Driver Classpublic class GFG {     // Main method    static void Main(String[] args)    {        // Creating object of Student structure        Student s1 = new Student("Ankita");        Student s2 = new Student("Soniya");        Student s3 = new Student("Ankita");         // Check the given objects are equal or not        Console.WriteLine("Object 1 is equal to object 2: {0}",                                                s1.Equals(s2));         Console.WriteLine("Object 1 is equal to object 3: {0}",                                                s1.Equals(s3));    }}
Object 1 is equal to object 2: False
Object 1 is equal to object 3: True
 
This method is used to check whether this instance and another specified String object have the same value. This method also performs an ordinal comparison in both case-sensitive and culture-insensitive.
Syntax: 
public bool Equals (string item);
Here, item is a string that is used to compare with this instance.
Return Value: The return type of this method is System.Boolean. If the value of item is same as the value of this instance, then this method will return true, otherwise, return false. And if the value of item is null then this method will return false.
Example:
C#
// C# program to illustrate// Equals(String) methodusing System; class GFG {     // Main method    static void Main(String[] args)    {         // Creating object of Student structure        string s1 = "GeeksforGeeks";        string s2 = "hellogeeksforgeeks";        string s3 = "GeeksforGeeks";         // Check the given strings are equal or not        Console.WriteLine("String 1 is equal to String 2: {0}",                                                s1.Equals(s2));         Console.WriteLine("String 1 is equal to String 3: {0}",                                                s1.Equals(s3));    }}
String 1 is equal to String 2: False
String 1 is equal to String 3: True
 
Next: Set-2 and Set-3Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.equals?view=netframework-4.7.2 
nidhi_biet
arorakashish0911
CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Class and Object
Lambda Expressions in C#
HashSet in C# with Examples
C# | How to use strings in switch statement
C# | Replace() Method
Collections in C#
C# | Abstract Classes
C# | Data Types
Introduction to .NET Framework
C# | Math.Floor() Method