Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can CultureInfo.CurrentCulture ever be null?

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
like image 712
Colonel Panic Avatar asked Nov 13 '12 10:11

Colonel Panic


People also ask

What is CultureInfo CurrentCulture?

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.

What is difference between CurrentCulture and CurrentUICulture?

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.

What is default culture in C#?

NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture.

What is CultureInfo Invariantculture?

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

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 the Thread.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 not null.

  • By calling the Windows GetUserDefaultLocaleName function.

Again, this doesn't leave open the option of a null value.

like image 95
Jon Avatar answered Sep 29 '22 08:09

Jon