可以CultureInfo.CurrentCulture永远是空? [英] Can CultureInfo.CurrentCulture ever be null?

查看:264
本文介绍了可以CultureInfo.CurrentCulture永远是空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以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如果你尝试将其设置为,所以这是合乎逻辑的假设抛出一个异常,有一个非 - 价值是不变的。

除此之外, 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 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.

这篇关于可以CultureInfo.CurrentCulture永远是空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆