views:

713

answers:

5

Could anybody raise an example to demonstrate the usage of Invariant Culture? I don't understnd what the documents describe I have read.

A: 

You can think of it as a fallback Culture. If your application does not support localization you can simply assume that it is running with InvariantCulture.

Ikaso
+14  A: 

A fake culture based on english with defined behavior. Great to write out, for example, stuff into config files so it can be read an written regardless of the culture the user has defined.

Basically it is a specific culture that is artificial and WILL NOT CHANGE.

TomTom
Specifically, you won't be caught out by different uses of commas and points in the string forms of numbers, or with odd case conversions.
Steve Gilham
Yesss... typical problem for:* American programmers which think the word is english ;) And then german customers write 1.000,00 for 1000 ;) Ouch.* Even in the same language, Switzerland and Germany for example use "." and "," in different ways in numbers.Result -> Config files are garbage.Use Invariant Language there ;)
TomTom
To add to Steve's comment: it's the culture to use when you don't actually care how things *look* (you don't care whether it uses commas or points or whatever, because the user will never see it) but you do need it always to be the same (e.g. because you need to be able to read in what you've written).
itowlson
+1  A: 

It is used for stuff that is the same regardless of culture (that doesn't need to be translated to some culture X to be appropriate)

as for an example - http://msdn.microsoft.com/en-us/library/4c5zdc6a.aspx. When you write out an app-specific file which the user shouldn't be messing around with, you should use InvariantCulture for all methods that take in a culture parameter.

However, an application should use the invariant culture only for processes that require culture-independent results, such as formatting and parsing data that is persisted to a file.
Gishu
+1  A: 

Using the InvariantCulture Property

The InvariantCulture property represents neither a neutral nor a specific culture. It represents a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region. Your applications can use this property with almost any method in the System.Globalization namespace that requires a culture. However, an application should use the invariant culture only for processes that require culture-independent results, such as formatting and parsing data that is persisted to a file. In other cases, it produces results that might be linguistically incorrect or culturally inappropriate.

Security Considerations If a security decision will be made based on the result of a string comparison or case change, your application should use an ordinal comparison that ignores case instead of using InvariantCulture. The default implementations of methods such as Compare()()() and ToUpper use the CurrentCulture property. Code that performs culture-sensitive string operations can cause security vulnerabilities if CurrentCulture is changed or if the culture on the computer running the code differs from the culture used to test the code. The behavior that you expect when writing a string operation differs from the actual behavior of your code on the executing computer. In contrast, an ordinal comparison depends solely on the binary value of the compared characters.

String Operations If your application needs to perform a culture-sensitive string operation that is not affected by the value of CurrentCulture, it should use a method that accepts a CultureInfo parameter. The application should specify the value of the InvariantCulture property for this parameter. The application should use the property with methods such as Compare()()() and ToUpper to eliminate cultural variations and ensure consistent results. For more information about using the InvariantCulture property to perform culture-insensitive string operations, see Culture-Insensitive String Operations.

Persisting Data The InvariantCulture property is useful for storing data that will not be displayed directly to users. Storing data in a culture-independent format guarantees a known format that does not change. When users from different cultures access the data, it can be formatted appropriately based on specific user. For example, if your application stores DateTime types in a text file, formatted for the invariant culture, the application should use the InvariantCulture property when calling ToString to store the strings and the Parse method to retrieve the strings. This technique ensures that the underlying values of the DateTime types do not change when the data is read or written by users from different cultures.

Ref.

Mitch Wheat
+2  A: 

Invariant culture is a special culture that you can always use in any .NET application. It is very useful in several flows, for example serialization: you can have 1,1 value in one culture and 1.1 in another. If you will try to parse "1,1" value in the culture with "." decimal symbol then parsing will fail. However you can use Invariant culture to convert number to string and parse it back - this will definatly work on any computer with any culture set.

        // Use some non invariant culture.
        CultureInfo nonInvariantCulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentCulture = nonInvariantCulture;

        decimal dec = 1.1m;
        string convertedToString = dec.ToString();

        // Simulate anoher culture being used, following code can run on another computer.
        nonInvariantCulture.NumberFormat.NumberDecimalSeparator = ",";

        decimal parsedDec;

        try
        {
            // This fails because value cannot be parsed.
            parsedDec = decimal.Parse(convertedToString);
        }
        catch (FormatException)
        {
        }

        // However you always can use Invariant culture:
        convertedToString = dec.ToString(CultureInfo.InvariantCulture);

        // This will always work because you serialized with the same culture.
        parsedDec = decimal.Parse(convertedToString, CultureInfo.InvariantCulture);
Andrew Bezzub