Could anybody raise an example to demonstrate the usage of Invariant Culture? I don't understnd what the documents describe I have read.
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.
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.
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.
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.
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);