What does CultureInfo.InvariantCulture mean?
I have a string of text like so:
var foo = "FooBar";
I want to declare a second string called bar
and make this equal to first and fourth character of my first foo
, so I do this like so:
var bar = foo[0].ToString() + foo[3].ToString();
This works as expected, but ReSharper is advising me to put Culture.InvariantCulture
inside my brackets, so this line ends up like so:
var bar = foo[0].ToString(CultureInfo.InvariantCulture)
+ foo[3].ToString(CultureInfo.InvariantCulture);
What does this mean, and will it affect how my program runs?
Not all cultures use the same format for dates and decimal / currency values.
This will matter for you when you are converting input values (read) that are stored as strings to DateTime
, float
, double
or decimal
. It will also matter if you try to format the aforementioned data types to strings (write) for display or storage.
If you know what specific culture that your dates and decimal / currency values will be in ahead of time, you can use that specific CultureInfo
property (ie CultureInfo("en-GB")
). For example if you expect a user input.
The CultureInfo.InvariantCulture
property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings.
The default value is CultureInfo.InstalledUICulture
so the default CultureInfo is depending on the executing OS's settings. This is why you should always make sure the culture info fits your intention (see Martin's answer for a good guideline).
When numbers, dates and times are formatted into strings or parsed from strings a culture is used to determine how it is done. Eg in the dominant en-US
culture you have these string representations:
In my culture ( da-DK
) the values have this string representation:
In the Windows operating system the user may even customize how numbers and date/times are formatted and may also choose another culture than the culture of his operating system. The formatting used is the choice of the user which is how it should be.
So when you format a value to be displayed to the user using for instance ToString
or String.Format
or parsed from a string using DateTime.Parse
or Decimal.Parse
the default is to use the CultureInfo.CurrentCulture
. This allows the user to control the formatting.
However, a lot of string formatting and parsing is actually not strings exchanged between the application and the user but between the application and some data format (eg an XML or CSV file). In that case you don't want to use CultureInfo.CurrentCulture
because if formatting and parsing is done with different cultures it can break. In that case you want to use CultureInfo.InvariantCulture
(which is based on the en-US
culture). This ensures that the values can roundtrip without problems.
The reason that ReSharper gives you the warning is that some application writers are unaware of this distinction which may lead to unintended results but they never discover this because their CultureInfo.CurrentCulture
is en-US
which has the same behavior as CultureInfo.InvariantCulture
. However, as soon as the application is used in another culture where there is a chance of using one culture for formatting and another for parsing the application may break.
So to sum it up:
CultureInfo.CurrentCulture
(the default) if you are formatting or parsing a user string. CultureInfo.InvariantCulture
if you are formatting or parsing a string that should be parseable by a piece of software. According to Microsoft:
The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.
(from http://msdn.microsoft.com/en-us/library/4c5zdc6a(vs.71).aspx)
So InvariantCulture is similair to culture "en-US" but not exactly the same. If you write:
var d = DateTime.Now;
var s1 = d.ToString(CultureInfo.InvariantCulture); // "05/21/2014 22:09:28"
var s2 = d.ToString(new CultureInfo("en-US")); // "5/21/2014 10:09:28 PM"
then s1 and s2 will have similair format but InvariantCulture adds leading zeroes and "en-US" uses AM or PM.
So InvariantCulture is better for internal usage, when you eg saves a date to a text-file or parses data. And a specified CultureInfo is better when you present data (date, currency...) to the end user.
链接地址: http://www.djcxy.com/p/75108.html