C# String Equals Method Example

In this source code example, we will demonstrate the usage of String.Equals() method in C# with an example.

The C# String.Equals() method is used to check whether two specified String objects have the same value or not. If both strings have the same value, it returns true otherwise false.

In other words, it is used to compare two strings on the basis of content.

C# String Equals Method Example

The following example demonstrates the String.Equals() method:

using System;

public class StringDemo
{
    public static void Main(string[] args)
    {
        string s1 = "sourcecodeexamples";
        string s2 = "sourcecodeexamples";
        string s3 = "examples";
        
        Console.WriteLine(s1.Equals(s2));  
        Console.WriteLine(s1.Equals(s3));  

    }

}

Output:

True
False

Related C# String Examples

References

https://docs.microsoft.com/en-us/dotnet/api/system.string.equals?view=net-6.0


Comments