gpt4 book ai didi

.net - CultureInfo.CurrentCulture 可以为空吗?

转载 作者:行者123 更新时间:2023-12-04 01:40:05 24 4
gpt4 key购买 nike

CultureInfo.CurrentCulture 可以为空吗?

空值会使我的程序崩溃,这是我不想要的。所以我问,为了安全起见,我需要做什么吗?

var culture = CultureInfo.CurrentCulture ?? CultureInfo.InvariantCulture

最佳答案

看起来肯定不是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 如果您尝试将其设置为 null,则会引发异常,因此假设具有非 null 是合乎逻辑的值是一个不变量。

除此之外, CultureInfo.CurrentCulture给出确定其初始值的算法:

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.



同样,这不会打开 null 的选项。值(value)。

关于.net - CultureInfo.CurrentCulture 可以为空吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13359141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com