Search
Enlightenment
  • Beginning iPhone 3 Development: Exploring the iPhone SDK
    Beginning iPhone 3 Development: Exploring the iPhone SDK
    by Dave Mark, Jeff LaMarche
  • Code Complete: A Practical Handbook of Software Construction
    Code Complete: A Practical Handbook of Software Construction
    by Steve McConnell
  • How to Break Software: A Practical Guide to Testing W/CD
    How to Break Software: A Practical Guide to Testing W/CD
    by James A. Whittaker
  • How Linux Works: What Every Superuser Should Know
    How Linux Works: What Every Superuser Should Know
    by Brian Ward
  • Rootkits: Subverting the Windows Kernel
    Rootkits: Subverting the Windows Kernel
    by Greg Hoglund, Jamie Butler
Blog Archives
Powered By

Want an awesome looking site like mine? Look no further than Squarespace.

Looking for a Web Host or a Domain Name registrar? Check out Bihira.

« BSOD Easter Egg in Mac OSX | Main | Twitter Got Hacked »
Saturday
17Jan2009

Comparing Strings in C#

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...

  1. 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.
  2. 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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

Thanks for that!

February 7, 2009 | Unregistered Commentermusicisall

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>