Part 4: We use Compare, CompareOrdinal and CompareTo to get an int indicating whether the first string is larger or smaller alphabetically.
C# program that compares strings for equality
static void Main()
string a = "a" + 1;
string b = "a" + 1;
// Part 1: use instance Equals() method.
Console.WriteLine("a.Equals(b) = true");
// Part 2: use instance method on other string.
Console.WriteLine("b.Equals(a) = true");
// Part 3: use op_Equality.
if (a == b)
Console.WriteLine("a == b = true");
// Part 4: use Compare methods.
// ... This returns zero if the strings are equal.
if (string.Compare(a, b) == 0)
Console.WriteLine("string.Compare(a, b) = 0");
// ... CompareOrdinal returns 0 if numeric values equal.
if (string.CompareOrdinal(a, b) == 0)
Console.WriteLine("string.CompareOrdinal(a, b) = 0");
// ... Use instance Compare method.
if (a.CompareTo(b) == 0)
Console.WriteLine("a.CompareTo(b) = 0");
a.Equals(b) = true
b.Equals(a) = true
a == b = true
string.Compare(a, b) = 0
string.CompareOrdinal(a, b) = 0
a.CompareTo(b) = 0
IL. Let us see what the instance Equals method and the equality operator do internally. The Equals method is found in mscorlib.dll. It has the following internal code.
Equals instance method on string type: C#// 1. Equals instance method
public bool Equals(string value)
if ((value == null) && (this != null))
return EqualsHelper(this, value);
Equality operator on string type: IL// The == operator overload MSIL:
.method public hidebysig specialname static bool
op_Equality(string a, string b) cil managed
L_0002: call bool System.String::Equals(
// Calls into this:
public static bool Equals(string a, string b)
return ((a == b) || (((a != null) &&
(b != null)) && EqualsHelper(a, b)));
Benchmark. To test the code for Equals methods, I ran a benchmark of 2 strings that are equal but don't share a string pool reference. This avoids the intern pool.string.Intern
Results: The performance of Equals and the equality operator are about the same. The clearest code is the best option here.
Info: In older .NET Frameworks, the equality operator was faster, but I cannot reproduce this result in 2019.
So: Prefer comparing 2 strings with "==" when possible, as it is clear and commonly done in C# programs.
Compare. Next, we review the Compare methods. In the first example in this article, you can see the result of the string.Compare method being tested for a value of zero.
Tip: When string.Compare returns zero, the strings are equal. It returns 1 or -1 depending on the sorted order of the string parameters.
Info: This means that if string.Compare ever returns 1 or -1, the strings are not equal.
Empty strings. You will find that, as FxCop recommends, testing against Length is usually the best way. The string.IsNullOrEmpty method deserves a special mention—it is fast and safe.IsNullOrEmpty, IsNullOrWhiteSpace
Cases. You can easily compare strings in such a way that their case differences are ignored. We use values of the StringComparison enum.
Tip: To do this, use the StringComparison.OrdinalIgnoreCase or other culture-specific methods in the Equals method.