System.String.Equals Method

Determines whether two specified string objects have the same value.

Syntax

public static bool Equals (string a, string b)

Parameters

a
The first string to compare, or null.
b
The second string to compare, or null.

Returns

true if the value of a is the same as the value of b; otherwise, false. If both a and b are null, the method returns true.

Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

Example

The following example demonstrates checking to see if two strings are equal.

C# Example

using System;
public class StringEqualsExample {
 public static void Main() {
 string strA = "A string";
 string strB = "a string";
 string strC = "a string";
 Console.Write( "The string '{0}' is equal to the string '{1}'? ", strA, strB );
 Console.WriteLine( String.Equals( strA, strB ) );
 Console.Write( "The string '{0}' is equal to the string '{1}'? ", strC, strB );
 Console.WriteLine( String.Equals( strC, strB ) );
 }
}
   

The output is

The string 'A string' is equal to the string 'a string'? False
The string 'a string' is equal to the string 'a string'? True

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0