185

Could anybody give an example to demonstrate the usage of the Invariant Culture? I don't understand what the documentation describes.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Ricky
  • 34,377
  • 39
  • 91
  • 131

5 Answers5

145

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);
jpaugh
  • 6,634
  • 4
  • 38
  • 90
Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • 10
    For what it's worth, this is in the `System.Globalization` namespace. – Jim H. Jul 23 '13 at 01:25
  • 1
    *This will always work because you serialized with the same culture* << this didn't make sense to me, I think I misunderstood something. If using `CultureInfo.InvariantCulture` works because you serialized with the same culture....then isn't it the same as using `CultureInfo.CurrentCulture`? – Alisson Reinaldo Silva Feb 25 '17 at 05:25
  • 3
    @Alisson If you put Andrew's last two code statements back into the original flow, I think it will make more sense. If you always serialize using `CultureInfo.InvariantCulture` then you know that you will always be able to parse back to a decimal using `CultureInfo.InvariantCulture`. If you use `CultureInfo.CurrentCulture` to serialize, your user might have "en-US" as their current culture. The next user that goes to parse it back to a decimal might have a current culture that uses "," for a separator. – David Specht Apr 28 '17 at 13:23
  • 2
    @Andrew Maybe I'm dense, but I couldn't understand what you were saying, without David's comment. I edited your answer to be explicit. – jpaugh Jun 13 '17 at 16:02
118

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.

John Smith
  • 7,243
  • 6
  • 49
  • 61
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 4
    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 Mar 11 '10 at 08:04
  • 8
    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 Mar 11 '10 at 08:05
  • 24
    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 Mar 11 '10 at 08:38
8

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Gishu
  • 134,492
  • 47
  • 225
  • 308
  • 3
    This speaks to the corollary that the Invariant Culture is only intended to be an intermediate format, i.e. if you are displaying stuff to the user that is the output of the invariant culture (that is, 'in' the invariant culture), you are doing something wrong and don't understand invariant culture. – mlhDev Dec 18 '15 at 03:14
3

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.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
0

It is a universal simple non-regional-specific English language and other related info. It’s like the language of the programming language itself. You can rely on it in setting up a universal calendar; in situation where you need to generate controller names, URL's, delegate names ...etc. and need things to act naturally and universally among all users.

Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54