String.Equals Method (Object)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Determines whether this instance of String and a specified object, which must also be a String object, have the same value.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- obj
- Type: System.Object
The string to compare to this instance.
Return Value
Type: System.Booleantrue if obj is a String and its value is the same as this instance; otherwise, false.
The following code example demonstrates the Equals method.
// Sample for String.Equals(Object) // String.Equals(String) // String.Equals(String, String) using System; using System.Text; class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { StringBuilder sb = new StringBuilder("abcd"); String str1 = "abcd"; String str2 = null; Object o2 = null; outputBlock.Text += "\n"; outputBlock.Text += String.Format(" * The value of String str1 is '{0}'.", str1) + "\n"; outputBlock.Text += String.Format(" * The value of StringBuilder sb is '{0}'.", sb.ToString()) + "\n"; outputBlock.Text += "\n"; outputBlock.Text += String.Format("1a) String.Equals(Object). Object is a StringBuilder, not a String.") + "\n"; outputBlock.Text += String.Format(" Is str1 equal to sb?: {0}", str1.Equals(sb)) + "\n"; outputBlock.Text += "\n"; outputBlock.Text += "1b) String.Equals(Object). Object is a String." + "\n"; str2 = sb.ToString(); o2 = str2; outputBlock.Text += String.Format(" * The value of Object o2 is '{0}'.", o2) + "\n"; outputBlock.Text += String.Format(" Is str1 equal to o2?: {0}", str1.Equals(o2)) + "\n"; outputBlock.Text += "\n"; outputBlock.Text += " 2) String.Equals(String)" + "\n"; outputBlock.Text += String.Format(" * The value of String str2 is '{0}'.", str2) + "\n"; outputBlock.Text += String.Format(" Is str1 equal to str2?: {0}", str1.Equals(str2)) + "\n"; outputBlock.Text += "\n"; outputBlock.Text += String.Format(" 3) String.Equals(String, String)") + "\n"; outputBlock.Text += String.Format(" Is str1 equal to str2?: {0}", String.Equals(str1, str2)) + "\n"; } } /* This example produces the following results: * The value of String str1 is 'abcd'. * The value of StringBuilder sb is 'abcd'. 1a) String.Equals(Object). Object is a StringBuilder, not a String. Is str1 equal to sb?: False 1b) String.Equals(Object). Object is a String. * The value of Object o2 is 'abcd'. Is str1 equal to o2?: True 2) String.Equals(String) * The value of String str2 is 'abcd'. Is str1 equal to str2?: True 3) String.Equals(String, String) Is str1 equal to str2?: True */
Show: