Comparing Strings in C#
January 17, 2009 Here's something that I never really gave much thought. In C#, there are multiple ways to compare two strings. But these different methods don't all yield the same result, particularly when a null value is involved. So I decided to write a few tests to see how the various methods differ.
Some of the info here should be common knowledge, but I think it's worth the time to take note of these different comparison methods.
string1.Equals(string2)
This first set of tests simply uses the Equals method.
string string1 = "TEST";
string string2 = null;
if(string1.Equals(string2))
The conditional in the above example will simply result as false, but what happens if we reverse string1 and string2?
string string1 = "TEST";
string string2 = null;
if(string2.Equals(string1))
You might initially expect this code to give the same results as the first example, but it doesn't. This example actually throws a System.NullReferenceException. Afterall, you can't call a method of a null object. So obviously this can be a dangerous method to use, because the results may vary depending on the "order" of the strings. So what else can we try?
string1 == string2
I think most programmers would initially turn to the basic == operator as their first solution.
string string1 = "TEST";
string string2 = null;
if(string1 == string2)
When using the == operator, the order of string1 and string2 don't matter. The expression will be false either way. So why would we ever use the Equals method rather than the == operator? Two reasons...
- When using the == operator, you can't take advantage of an Equals overload that let's you specify case-insensitivity. Instead you would have to use something like the ToLower method on both strings. While this isn't necessarily a big deal, it does make your code a little bit uglier. It also creates another place for a programmer to potentially make a mistake by forgetting to use ToLower on a string. A bug like that could easily go unnoticed for a long time.
- In most cases, you're not going to want a null string in your application. You may expect plenty of empty strings, but not null strings. So it could be argued that if you somehow end up with a null string then you want the application to throw an exception because you want to know about it! Using the == operator won't be able to give you that.
string.Equals(string1,string2)
This final example uses the string.Equals static method.
string string1 = "TEST";
string string2 = null;
if(string.Equals(string1,string2))
As far as results are concerned, this static method acts exactly the same way as the == operator. However, because the == operator performs an extra reference check behind the scenes, this method is actually slightly faster. You, as a developer, should not be concerned with such a minor optimization. Instead, do what you're most comfortable with. If you prefer to use the == operator, great. If you would rather use the static method, that works too.







Reader Comments (1)
Thanks for that!