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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With