Can CultureInfo.CurrentCulture ever be null?
Asked Answered
G

3

12

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
Giveaway answered 13/11, 2012 at 10:32 Comment(0)
A
16

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.

Altorilievo answered 13/11, 2012 at 10:35 Comment(0)
E
6

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.

Eiser answered 13/11, 2012 at 10:40 Comment(2)
the question was about CurrentCulture, not CurrentUICulture, although the situation there is most probably very similar.Medford
@Knagis: Yes, i've skipped a step accidentally, but it's valid anyway. Edited the answer.Eiser
P
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;
}
}
Pettitoes answered 13/11, 2012 at 10:35 Comment(1)
That's no evidence. The culture field could be set directly. Actually, if you look at the getter there are multiple places where the null is possible. Also, you're showing the instance property CurrentCultureand not the static as requested.Eiser

© 2022 - 2024 — McMap. All rights reserved.