There are a number of ways to compare strings. Are there performance gains by doing one way over another?
I\'ve always opted to compare strings like so:
According to Reflector
"Hello" == "World"
is the same as
String.Equals("Hello", "World");
which basically determines if they are the same reference object, if either of them is null, which would be an automatic false if one was null and the other was not, and then compares each character in an unsafe loop. So it doesn't care about cultural rules at all, which usually isn't a big deal.
and
"Hello".CompareTo("World") == 0
is the same as
CultureInfo.CurrentCulture.CompareInfo.Compare("Hello", "World", CompareOptions.None);
This is basically the opposite as far as functionality. It takes into consideration culture, encoding, and everything else with the string in to context.
So I would imagine that String.CompareTo is a couple of orders of magnitude slower than the equality operator.
as for your LINQ it doesn't matter if you are using LINQ-to-SQL because both will generate the same SQL
var results = from names in ctx.Names
where names.FirstName.CompareTo("Bob Wazowski") == 0
select names;
of
SELECT [name fields]
FROM [Names] AS [t0]
WHERE [t0].FirstName = @p0
so you really aren't gaining anything for LINQ-to-SQL except harder to read code and probably more parsing of the expressions. If you are just using LINQ for standard array stuff then the rules I laid out above apply.
To best way to compare string
's in C# is to use the a.Equals(b)
where a and b are strings. This is the best way to compare string because it compares the values of the objects a and b, and does not depent on the reference of the objects.
If you're going to use "==
" symbol, the result will be equal if both objects have the same reference but you will have a problem when they have different references and have the same value.
The compareTo
method is best way to use if your testing whether the other string is preceding, following or appearing in the same position of the other string wherein it will return negative value , positive value or zero value respectively. It will return also positive value if the parameter is null
In my opinion, you should always use the clearest way, which is using ==
!
This can be understood directly: When "Hello" equals "World" then do something.
if ("Hello" == "World")
// ...
Internally, String::Equals
is invoked which exists explicitly for this purpose - Comparing two strings for equality. (This has nothing to do with pointers and references etc.)
This here isn't immediately clear - Why compare to zero?
if ("Hello".CompareTo("World") == 0)
.CompareTo isn't designed just for checking equality (you have == for this) - It compares two strings. You use .CompareTo in sorts to determine wheter one string is "greater" than another. You can check for equality because it yield zero for equal strings, but that's not what it's concepted for.
Hence there are different methods and interfaces for checking equality (IEquatable, operator ==) and comparing (IComparable)
Linq doesn't behave different than regular C# here.
Well MSDN states you shoul use the comparision function according to the task you need to perform:
The CompareTo method was designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. To determine whether two strings are equivalent, call the Equals method.
So if its not about sorting and the retrun value is not important i would say one should use the:
first.Equals(second)
or if the comparison is culture specific for example in languages like in german:
String.Equals(first, second, StringComparison.CurrentCulture)
Take a look a these links:
How to: Compare Strings (C# Programming Guide)
String.CompareTo Method (Object)
I generally use String.Compare with the overload that takes a StringComparison parameter, because then I can be absolutely explicit about whether or not the comparison is case- and culture-sensitive. This needs .NET 2.0 or later.
The fastest is StringComparison.Ordinal (or StringComparison.OrdinalIgnoreCase if case-insensitive) for comparisons that are not culture-sensitive.
The problem with using == is that it's not clear that the author has considered case- and culture-sensitivity.
There's a good MSDN article on the subject here.
There is a nice article Comparing Values for Equality in .NET: Identity and Equivalence which is a bit more general than only string comparison, but very interesting nevertheless.