C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: There are small differences in the behavior of these statements. Parts 1 and 2 require that the string be non-null.
Operator: The most common method for string comparisons is part 3. Sometimes operator overloading is confusing.
And: It might obscure what the compiler actually does with the expression. The compiler calls an operator method.
OperatorFinally: We use Compare, CompareOrdinal and CompareTo to get int indicating whether the first string is larger or smaller alphabetically.
C# program that compares strings for equality
using System;
class Program
{
static void Main()
{
string a = "a" + 1;
string b = "a" + 1;
// 1
// Compare a to b using instance method on a.
if (a.Equals(b))
{
Console.WriteLine("a.Equals(b) = true");
}
// 2
// Compare b to a using instance method on b.
if (b.Equals(a))
{
Console.WriteLine("b.Equals(a) = true");
}
// 3
// Compare a to b with op_Equality
if (a == b)
{
Console.WriteLine("a == b = true");
}
// 4
// Compare with string.Compare; this returns zero if they are equal
if (string.Compare(a, b) == 0)
{
Console.WriteLine("string.Compare(a, b) = 0");
}
// 5
// Compare with string.CompareOrdinal
// This returns zero if the char numeric values are equal
if (string.CompareOrdinal(a, b) == 0)
{
Console.WriteLine("string.CompareOrdinal(a, b) = 0");
}
// 6
// Compare with instance Compare method
if (a.CompareTo(b) == 0)
{
Console.WriteLine("a.CompareTo(b) = 0");
}
}
}
Output
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
Equals instance method on string type: C#
// 1. Equals instance method
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public bool Equals(string value)
{
if ((value == null) && (this != null))
{
return false;
}
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
{
.maxstack 8
L_0000: ldarg.0
L_0001: ldarg.1
L_0002: call bool System.String::Equals(
string, string)
L_0007: ret
}
// Calls into this:
public static bool Equals(string a, string b)
{
return ((a == b) || (((a != null) &&
(b != null)) && EqualsHelper(a, b)));
}
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.
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.
Tip: To do this, use the StringComparison.OrdinalIgnoreCase or other culture-specific methods in the Equals method.
StringComparison, StringComparer