If you are using INVARIANT then you are probably MISusing it, #1

by Michael S. Kaplan, published on 2005/10/15 03:31 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2005/10/15/481314.aspx


(I am putting a #1 at the end of the title because I think this is going to be a recurring theme....)

Maybe you remember the original post from December of last year -- Where is the locale? "Its Invariant." In where?

Or maybe your introduction was in later posts like Comparison confusion: INVARIANT vs. ORDINAL or Invariant and Ordinal Redux.

On the other hand, maybe you have never read about LOCALE_INVARIANT or its managed equivalent (CultureInfo.InvariantCulture) on this blog.

No matter which category you fall into, or even if you fall into none of them, the invariant locale/culture is probably not what you want to use. It is a great way to do linguistic comparisons without having to tell people to "just use US English" but that is almost never what people do with it. People do things like compare filenames (or other symbolic identifiers) and get back inaccurate results. Or they assume that the invariant sort will never vary (even in between versions), which is not true. Or they are assuming it is like an ordinal sort (it is not).

Almost every single use of invariant that I have had opportunity to see to date (with the exception of ones from the GIFT team) has been incorrect. Which is impressively frightening enough as far as I am concerned that I would rather just say to not use it. AT ALL.

Keep in mind that this is not just me. If you look at the Official Guidance: New Recommendations for Strings in .NET 2.0, you will see that just about all of the situations where they used to recommend using invariant (to avoid Turkish 'I' problems) they are now recommending to people that they should use Ordinal or OrdinalIgnoreCase comparisons instead. Because as great as it is to avoid problems of inappropriate culturally sensitive comparisons, it is even better to avoid inappropriate linguistically sensitive comparisons, too.

So just say NO to invariant. Unless you really know what you are doing and why (and maye not even then, in case you are mistaken!).

 

This post brought to you by "ฐ" (U+0e10, a.k.a. THAI CHARACTER THI THAN)


# Jochen Kalmbach on 16 Oct 2005 2:09 PM:

The most usefull area of Invariant is saving/loading data from/to files.

By the way: The whole XmlConvert-class is Only using InvariantCulture (which makes scense)!

Greetings
Jochen

go to newer or older post, or back to index or month or day