可以CultureInfo.CurrentCulture永远是空? [英] Can CultureInfo.CurrentCulture ever be null?
问题描述
可以CultureInfo.CurrentCulture永远是空?
一个空值会崩溃我的程序,我不想要的。所以我问,为了安全起见,我需要做什么?
VAR文化= CultureInfo.CurrentCulture? CultureInfo.InvariantCulture
这肯定看起来像它的保证是无 - 空
:
文化是正在执行的线程的属性。这个只读 属性等同于获取返回的
的CultureInfo
对象 由Thread.CurrentCulture
属性。
<一个href="http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture.aspx"><$c$c>Thread.CurrentCulture$c$c>如果你尝试将其设置为空
,所以这是合乎逻辑的假设抛出一个异常,有一个非 - 空
价值是不变的。
除此之外, CultureInfo.CurrentCulture
给出了算法来决定它的初始值:
如何线程的文化确定
当一个线程启动时,它的文化被初步确定为 如下:
通过检索由
DefaultThreadCurrentCulture
属性在应用领域指定的文化 该线程执行,如果属性值不是空
。通过调用Windows
GetUserDefaultLocaleName
的功能。
同样,这不会留下打开空
值的选项。
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
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.
这篇关于可以CultureInfo.CurrentCulture永远是空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!