Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use the Invariant Culture?

Is it inappropriate to use InvariantCulture? And, if used, would it affect any global settings?

Dim hh As Decimal = 123456789.123456

' Is this an appropriate usage?
Dim ll As String = hh.ToString("N4", CultureInfo.InvariantCulture)
like image 434
Hello-World Avatar asked Jun 26 '12 18:06

Hello-World


People also ask

What does invariant culture do?

The InvariantCulture property can be used to persist data in a culture-independent format. This provides a known format that does not change and that can be used to serialize and deserialize data across cultures.

What is the use of CultureInfo in C#?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.

What is datetime CultureInfo?

A DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.


Video Answer


2 Answers

Is using CultureInfo.InvariantCulture bad?

It entirely depends on what you're trying to achieve. If you're trying to format values to be machine-readable, it's almost always the right thing to do. If you're trying to format them for users, it may well be inappropriate.

Will it effect the global culture?

Specifying the invariant culture in a method call won't change the current thread's current culture, if that's what you're worried about.

like image 177
Jon Skeet Avatar answered Oct 26 '22 23:10

Jon Skeet


Is using CultureInfo.InvariantCulture bad?

No. It exists for a reason. It allows you to parse text in a consistent, reliable manner, no matter what the current system culture happens to be.

However, it shouldn't be used (normally) to parse user input, but rather parsing text where you know it was written in the same manner that InvariantCulture expects.

like image 22
Reed Copsey Avatar answered Oct 26 '22 23:10

Reed Copsey