问题
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
回答1:
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.
回答2:
No, non-null is guaranteed. This is the implementation of System.Threading.Thread.CurrentCulture
which is returned directly from CultureInfo.CurentCulture
(via ILSpy):
// System.Threading.Thread
public CultureInfo CurrentUICulture
{
[SecuritySafeCritical]
get
{
if (this.m_CurrentUICulture == null)
{
return CultureInfo.UserDefaultUICulture;
}
CultureInfo cultureInfo = null;
if (!Thread.nativeGetSafeCulture(this, Thread.GetDomainID(), true, ref cultureInfo) || cultureInfo == null)
{
return CultureInfo.UserDefaultUICulture;
}
return cultureInfo;
}
// setter following
So if m_CurrentUICulture
is null it will return UserDefaultUICulture
.
This is the source:
internal static CultureInfo UserDefaultUICulture
{
get
{
CultureInfo cultureInfo = CultureInfo.s_userDefaultUICulture;
if (cultureInfo == null)
{
CultureInfo.s_userDefaultUICulture = CultureInfo.InvariantCulture;
cultureInfo = CultureInfo.InitUserDefaultUICulture();
CultureInfo.s_userDefaultUICulture = cultureInfo;
}
return cultureInfo;
}
}
As you can see, if even that is null CultureInfo.InvariantCulture
will be returned.
回答3:
Looking at the .NET source, it will throw an error if anybody tries to set it to null
- so no, it cannot be null
.
/// <summary>Gets or sets the culture for the current thread.</summary>
/// <returns>A <see cref="T:System.Globalization.CultureInfo" /> representing the culture for the current thread.</returns>
/// <exception cref="T:System.NotSupportedException">The property is set to a neutral culture. Neutral cultures cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.</exception>
/// <exception cref="T:System.ArgumentNullException">The property is set to null.</exception>
/// <filterpriority>2</filterpriority>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlThread" />
/// </PermissionSet>
[__DynamicallyInvokable]
public CultureInfo CurrentCulture
{
[__DynamicallyInvokable]
get
{
if (AppDomain.IsAppXModel())
{
return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? this.GetCurrentCultureNoAppX();
}
return this.GetCurrentCultureNoAppX();
}
[__DynamicallyInvokable, SecuritySafeCritical]
[SecurityPermission(SecurityAction.Demand, ControlThread = true)]
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
CultureInfo.nativeSetThreadLocale(value.SortName);
value.StartCrossDomainTracking();
this.m_CurrentCulture = value;
}
}
来源:https://stackoverflow.com/questions/13359141/can-cultureinfo-currentculture-ever-be-null