Hello-World
Hello-World

Reputation: 9555

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)

Upvotes: 5

Views: 592

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1504172

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.

Upvotes: 7

Reed Copsey
Reed Copsey

Reputation: 564931

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.

Upvotes: 6

Related Questions