System.String.Equals Method

Determines whether this instance and a specified object, which must also be a string object, have the same value.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public override bool Equals (object obj)

Parameters

obj
The string to compare to this instance.

Returns

true if obj is a string and its value is the same as this instance; otherwise, false.

Exceptions

TypeReason
NullReferenceExceptionThe current instance is a null reference.

Remarks

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

Example

The following example demonstrates checking to see if an object is equal to the current instance.

C# Example

using System;
public class StringEqualsExample {
 public static void Main() {
 string str = "A string";
 Console.WriteLine( "The given string is '{0}'", str );
 Console.Write( "The given string is equal to 'A string'? " );
 Console.WriteLine( str.Equals( "A string" ) );
 Console.Write( "The given string is equal to 'A String'? " );
 Console.WriteLine( str.Equals( "A String" ) );
 }
}
   

The output is

The given string is 'A string'
The given string is equal to 'A string'? True
The given string is equal to 'A String'? False

Requirements

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