Use the Equals() method and equality operator to check if two strings are equal in CSharp

Description

The following code shows how to use the Equals() method and equality operator to check if two strings are equal.

Example


  //from  w  w w  .  j a v  a 2 s  .c  om
  
  
     

using System;

class MainClass {

    public static void Main() {

        string myString = String.Concat("Friends, ", "Romans");
        string myString2 = String.Concat("Friends");
        bool boolResult;
        boolResult = String.Equals("bbc", "bbc");
        Console.WriteLine("String.Equals(\"bbc\", \"bbc\") is " + boolResult);
        boolResult = myString.Equals(myString2);
        Console.WriteLine("myString.Equals(myString2) is " + boolResult);
        boolResult = myString == myString2;
        Console.WriteLine("myString == myString2 is " + boolResult);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var