New Best Practices for working with Strings

Posted Monday, July 18, 2005 1:17 PM by C-Dog's .NET Tip of the Day
In Famework version 2.0, some new properties have been added which make comparing strings less buggy and more efficient.  A new third parameter has been added which takes type of StringComparison.  There are several possible values, but the most common ones are Ordinal, OrdinalIgnoreCase, InvariantCulture, and CurrentCulture.  Using the right one of these at the right time will allow for the best performance. 
 
Ordinal tell String.Compare to do a byte-for-bye comparison that must match exactly.  OrdinalIgnoreCase does a comparison where the the case is ignored.  This is good for things such as file paths, registry keys, or anything where you don't care about the case of the letters.  InvariantCulture is not typically used.  CurrentCulture  is used typically for any data that is displayed to the customer and user input. 
 
As a best practice, strings should always be compared using one of the possible values using String.Compare.  For more information on string comparison see the URL below.
 

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=92