Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to compare two CultureInfo instances?

Background:

I have a problem with a component, which changes the current thread culture to "en-US" every time after a call to a specific method of it. Further in my process that leads to problems, e.g. the data access layer is no more working because SqlParameter's CultureInfo gets also changed to "en-US", thus a given string can't be parsed no more to a DateTime SqlValue.

Possible solution:

So the idea is to backup the threads current culture before and restore it afterwards the call to the specific method which changes the current threads culture. When restoring the culture first i check for the culture if it has changed at all.

The Problem/Question:

I could compare the backuped CultureInfo.Name with Thread.CurrentThread.CurrentCulture.Name but I could also use the .Equals() method of the CultureInfo instance. Which is the better way for comparing two CultureInfo instances? Is there maybe a third/better solution?

like image 974
Kr15 Avatar asked Jun 25 '14 15:06

Kr15


People also ask

What is CultureInfo?

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 invariant culture?

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.


1 Answers

You should use (as it is overloaded for comparing CultureInfo instances)

bool result2 = cultureInfo1.Equals(cultureInfo2); 

As shown in this blog: http://www.toolheaven.net/post/2010/07/02/Beware-when-comparing-CultureInfo-instances.aspx

like image 167
Rico Suter Avatar answered Sep 28 '22 07:09

Rico Suter