tne
tne

Reputation: 7261

Identity comparisons of `CultureInfo` objects retrieved using `CultureInfo.GetCultureInfo`

Is it always safe to compare CultureInfo objects retrieved with CultureInfo.GetCultureInfo using the identity operator (==) ?

In other words, is the cache guaranteed to always return the same objects, or is it possible that it might drop entries and recreate them as new objects at some later point?

Note that the documentation for its overloads isn't helpful in that regard, and the alternative is of course to use the Object.Equals method.

Upvotes: 1

Views: 64

Answers (1)

tne
tne

Reputation: 7261

No.

There is a comment in the source of the private helper method for the overloads that reads:

// There is a race condition in this code with the side effect that the second thread's value
// clobbers the first in the dictionary. This is an acceptable ---- since the CultureInfo objects
// are content equal (but not reference equal). Since we make no guarantees there, this ---- is
// acceptable.

Funny that they felt the need to censor what is most likely the word "hack".

Upvotes: 1

Related Questions