Comparing string in C# is pretty simple. In fact there are several ways to do it. I have listed some in the block below. What I am curious about are the differences between them and when one should be used over the others? Should one be avoided at all costs? Are there more I haven't listed?
string testString = "Test";
string anotherString = "Another";
if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}
(Note: I am looking for equality in this example, not less than or greater than but feel free to comment on that as well)
with .Equals, you also gain the StringComparison options. very handy for ignoring case and other things.
btw, this will evaluate to false
Since == compares the values of a and b (which are pointers) this will only evaluate to true if the pointers point to the same object in memory. .Equals dereferences the pointers and compares the values stored at the pointers. a.Equals(b) would be true here.
and if you change b to:
then a.Equals(b) is false, but
would be true
a.CompareTo(b) calls the string's CompareTo function which compares the values at the pointers and returns <0 if the value stored at a is less than the value stored at b, returns 0 if a.Equals(b) is true, and >0 otherwise. However, this is case sensitive, I think there are possibly options for CompareTo to ignore case and such, but don't have time to look now. As others have already stated, this would be done for sorting. Comparing for equality in this manner would result in unecessary overhead.
I'm sure I'm leaving stuff out, but I think this should be enough info to start experimenting if you need more details.
Not that performance usually matters with 99% of the times you need to do this, but if you had to do this in a loop several million times I would highly suggest that you use .Equals or == because as soon as it finds a character that doesn't match it throws the whole thing out as false, but if you use the CompareTo it will have to figure out which character is less than the other, leading to slightly worse performance time.
If your app will be running in different countries, I'd recommend that you take a look at the CultureInfo implications and possibly use .Equals. Since I only really write apps for the US (and don't care if it doesn't work properly by someone), I always just use ==.
In the forms you listed here, there's not much difference between the two.
CompareTo
ends up calling aCompareInfo
method that does a comparison using the current culture;Equals
is called by the==
operator.If you consider overloads, then things get different.
Compare
and==
can only use the current culture to compare a string.Equals
andString.Compare
can take aStringComparison
enumeration argument that let you specify culture-insensitive or case-insensitive comparisons. OnlyString.Compare
allows you to specify aCultureInfo
and perform comparisons using a culture other than the default culture.Because of its versatility, I find I use
String.Compare
more than any other comparison method; it lets me specify exactly what I want.Using .Equals is also a lot easier to read.