The invariant culture is a special culture which is useful because it will not change. The current culture can change from one user to another, or even from one run to another, so you can't rely on it staying the same.
Being able to use the same culture each time is very important 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 second culture, then parsing will fail. However you can use the invariant culture to convert a number to a string and later parse it back from 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 another 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);
A fake culture based on English with defined behavior. Great to write out, for example, stuff into config files so it can be read and 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 - https://msdn.microsoft.com/en-us/library/4c5zdc6a(v=vs.100).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.
Note that per the docs linked above:
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.
The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.
CultureInfo.InvariantCulture retrieves an instance of the invariant culture. It can be used in almost any method in the System.Globalization namespace that requires a culture.
The objects returned by properties such as CompareInfo, DateTimeFormat, and NumberFormat also reflect the string comparison and formatting conventions of the invariant culture. The InvariantCulture property comes handy when you want to display persist data in a culture-independent format.
For instance, if you want to display a number or datetime in a specific format independent of the application's current culture you can use CultureInfo.InvariantCulture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With