I have some code like this:
If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then
DoSomething()
End If
I don't care about the case. Should I use OrdinalIgnoreCase, InvariantCultureIgnoreCase, or CurrentCultureIgnoreCase?
I have some code like this:
If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then
DoSomething()
End If
I don't care about the case. Should I use OrdinalIgnoreCase, InvariantCultureIgnoreCase, or CurrentCultureIgnoreCase?
Newer .Net Docs now has a table to help you decide which is best to use in your situation.
From MSDN's "New Recommendations for Using Strings in Microsoft .NET 2.0"
Summary: Code owners previously using the
InvariantCulturefor string comparison, casing, and sorting should strongly consider using a new set ofStringoverloads in Microsoft .NET 2.0. Specifically, data that is designed to be culture-agnostic and linguistically irrelevant should begin specifying overloads using either theStringComparison.OrdinalorStringComparison.OrdinalIgnoreCasemembers of the newStringComparisonenumeration. These enforce a byte-by-byte comparison similar tostrcmpthat not only avoids bugs from linguistic interpretation of essentially symbolic strings, but provides better performance.
I guess it depends on your situation. Since ordinal comparisons are actually looking at the characters' numeric Unicode values, they won't be the best choice when you're sorting alphabetically. For string comparisons, though, ordinal would be a tad faster.
It depends on what you want, though I'd shy away from invariantculture unless you're very sure you'll never want to localize the code for other languages. Use CurrentCulture instead.
Also, OrdinalIgnoreCase should respect numbers, which may or may not be what you want.