Can CultureInfo.CurrentCulture ever be null?
A null value would crash my program, which I don't want. So I'm asking, to be safe, do I need to do?
var culture = CultureInfo.CurrentCulture ?? CultureInfo.InvariantCulture
The CultureInfo. CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. You get the culture of the current thread by retrieving the value of the CultureInfo. CurrentCulture property, as the following example illustrates. C# Copy.
CurrentCulture is the . NET representation of the default user locale of the system. This controls default number and date formatting and the like. CurrentUICulture refers to the default user interface language, a setting introduced in Windows 2000.
NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system 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.
It definitely looks like it's guaranteed to be non-null
:
The culture is a property of the executing thread. This read-only property is equivalent to retrieving the
CultureInfo
object returned by theThread.CurrentCulture
property.
Thread.CurrentCulture
throws an exception if you try to set it to null
, so it's logical to assume that having a non-null
value is an invariant.
Apart from this, CultureInfo.CurrentCulture
gives the algorithm that determines its initial value:
How a Thread's Culture Is Determined
When a thread is started, its culture is initially determined as follows:
By retrieving the culture that is specified by the
DefaultThreadCurrentCulture
property in the application domain in which the thread is executing, if the property value is notnull
.By calling the Windows
GetUserDefaultLocaleName
function.
Again, this doesn't leave open the option of a null
value.
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