Rediger

Del via


CultureInfo Constructors

Definition

Initializes a new instance of the CultureInfo class.

Overloads

CultureInfo(Int32)

Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier.

CultureInfo(String)

Initializes a new instance of the CultureInfo class based on the culture specified by name.

CultureInfo(Int32, Boolean)

Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier and on a value that specifies whether to use the user-selected culture settings from Windows.

CultureInfo(String, Boolean)

Initializes a new instance of the CultureInfo class based on the culture specified by name and on a value that specifies whether to use the user-selected culture settings from Windows.

CultureInfo(Int32)

Source:
CultureInfo.cs
Source:
CultureInfo.cs
Source:
CultureInfo.cs

Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier.

public:
 CultureInfo(int culture);
public CultureInfo (int culture);
new System.Globalization.CultureInfo : int -> System.Globalization.CultureInfo
Public Sub New (culture As Integer)

Parameters

culture
Int32

A predefined CultureInfo identifier, LCID property of an existing CultureInfo object, or Windows-only culture identifier.

Exceptions

culture is less than zero.

culture is not a valid culture identifier. See the Notes to Callers section for more information.

Remarks

Predefined culture identifiers for cultures available on Windows system are listed in the Language tag column in the list of language/region names supported by Windows. Culture names follow the standard defined by BCP 47.

In most cases, the culture parameter is mapped to the corresponding National Language Support (NLS) locale identifier. The value of the culture parameter becomes the value of the LCID property of the new CultureInfo.

We recommend that you call the locale name constructor CultureInfo.CultureInfo, because locale names are preferable to LCIDs. For custom locales, a locale name is required.

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If the specified culture identifier matches the culture identifier of the current Windows culture, this constructor creates a CultureInfo that uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. If the user settings are incompatible with the culture associated with the CultureInfo (for example, if the selected calendar is not one of the OptionalCalendars) the results of the methods and the values of the properties are undefined.

If the specified culture identifier does not match the identifier of the current Windows culture, this constructor creates a CultureInfo that uses the default values for the specified culture.

The UseUserOverride property is always set to true.

For example, suppose that Arabic (Saudi Arabia) is the current Windows culture and the user has changed the calendar from Hijri to Gregorian.

For cultures that use the euro, .NET Framework and Windows XP set the default currency as euro. However, older versions of Windows do not. Therefore, if the user of an older version of Windows has not changed the currency setting through the regional and language options portion of Control Panel, the currency might be incorrect. To use the .NET Framework default setting for the currency, the application should use a CultureInfo constructor overload that accepts a useUserOverride parameter and set it to false.

Note

For backwards compatibility, a culture constructed using a culture parameter of 0x0004 or 0x7c04 will have a Name property of zh-CHS or zh-CHT, respectively. You should instead prefer to construct the culture using the current standard culture names of zh-Hans or zh-Hant, unless you have a reason for using the older names.

Notes to Callers

.NET Framework 3.5 and earlier versions throw an ArgumentException if culture is not a valid culture identifier. Starting with .NET Framework 4, this constructor throws a CultureNotFoundException. Starting with apps that run under .NET Framework 4 or later on Windows 7 or later, the method attempts to retrieve a CultureInfo object whose identifier is culture from the operating system; if the operating system does not support that culture, the method throws a CultureNotFoundException exception.

On .NET 6 and later versions, a CultureNotFoundException is thrown if the app is running in an environment where globalization-invariant mode is enabled, for example, some Docker containers, and a culture other than the invariant culture is specified.

See also

Applies to

CultureInfo(String)

Source:
CultureInfo.cs
Source:
CultureInfo.cs
Source:
CultureInfo.cs

Initializes a new instance of the CultureInfo class based on the culture specified by name.

public:
 CultureInfo(System::String ^ name);
public CultureInfo (string name);
new System.Globalization.CultureInfo : string -> System.Globalization.CultureInfo
Public Sub New (name As String)

Parameters

name
String

A predefined CultureInfo name, Name of an existing CultureInfo, or Windows-only culture name. name is not case-sensitive.

Exceptions

name is null.

name is not a valid culture name. For more information, see the Notes to Callers section.

Examples

The following example retrieves the current culture. If it is anything other than the French (France) culture, it calls the CultureInfo(String) constructor to instantiate a CultureInfo object that represents the French (France) culture and makes it the current culture. Otherwise, it instantiates a CultureInfo object that represents the French (Luxembourg) culture and makes it the current culture.

using System;
using System.Globalization;

public class ChangeEx1
{
    public static void Main()
    {
        CultureInfo current = CultureInfo.CurrentCulture;
        Console.WriteLine("The current culture is {0}", current.Name);
        CultureInfo newCulture;
        if (current.Name.Equals("fr-FR"))
            newCulture = new CultureInfo("fr-LU");
        else
            newCulture = new CultureInfo("fr-FR");

        CultureInfo.CurrentCulture = newCulture;
        Console.WriteLine("The current culture is now {0}",
                          CultureInfo.CurrentCulture.Name);
    }
}
// The example displays output like the following:
//     The current culture is en-US
//     The current culture is now fr-FR
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim current As CultureInfo = CultureInfo.CurrentCulture
      Console.WriteLine("The current culture is {0}", current.Name)
      Dim newCulture As CultureInfo
      If current.Name.Equals("fr-FR") Then
         newCulture = New CultureInfo("fr-LU")
      Else
         newCulture = new CultureInfo("fr-FR")
      End If

      CultureInfo.CurrentCulture = newCulture
      Console.WriteLine("The current culture is now {0}",
                        CultureInfo.CurrentCulture.Name)
   End Sub
End Module
' The example displays output like the following:
'     The current culture is en-US
'     The current culture is now fr-FR

Remarks

For a list of predefined culture names on Windows systems, see the Language tag column in the list of language/region names supported by Windows. Culture names follow the standard defined by BCP 47. In addition, starting with Windows 10, name can be any valid BCP-47 language tag.

If name is String.Empty, the constructor creates an instance of the invariant culture; this is equivalent to retrieving the value of the InvariantCulture property.

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If the culture identifier associated with name matches the culture identifier of the current Windows culture, this constructor creates a CultureInfo object that uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. If the user settings are incompatible with the culture associated with the CultureInfo, for example, if the selected calendar is not one of the OptionalCalendars, the results of the methods and the values of the properties are undefined.

If the culture identifier associated with name does not match the culture identifier of the current Windows culture, this constructor creates a CultureInfo object that uses the default values for the specified culture.

The UseUserOverride property is always set to true.

For example, suppose that Arabic (Saudi Arabia) is the current culture of Windows and the user changed the calendar from Hijri to Gregorian:

The LCID property of the new CultureInfo is set to the culture identifier associated with the specified name.

Notes to Callers

.NET Framework 3.5 and earlier versions throw an ArgumentException if name is not a valid culture name. Starting with .NET Framework 4, this constructor throws a CultureNotFoundException. Starting with apps that run under .NET Framework 4 or later on Windows 7 or later, the method attempts to retrieve a CultureInfo object whose identifier is name from the operating system; if the operating system does not support that culture, and if name is not the name of a supplementary or replacement culture, the method throws a CultureNotFoundException exception.

On .NET 6 and later versions, a CultureNotFoundException is thrown if the app is running in an environment where globalization-invariant mode is enabled, for example, some Docker containers, and a culture other than the invariant culture is specified.

See also

Applies to

CultureInfo(Int32, Boolean)

Source:
CultureInfo.cs
Source:
CultureInfo.cs
Source:
CultureInfo.cs

Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier and on a value that specifies whether to use the user-selected culture settings from Windows.

public:
 CultureInfo(int culture, bool useUserOverride);
public CultureInfo (int culture, bool useUserOverride);
new System.Globalization.CultureInfo : int * bool -> System.Globalization.CultureInfo
Public Sub New (culture As Integer, useUserOverride As Boolean)

Parameters

culture
Int32

A predefined CultureInfo identifier, LCID property of an existing CultureInfo object, or Windows-only culture identifier.

useUserOverride
Boolean

true to use the user-selected culture settings (Windows only); false to use the default culture settings.

Exceptions

culture is less than zero.

culture is not a valid culture identifier. See the Notes to Callers section for more information.

Remarks

Predefined culture identifiers available on Windows systems are listed in the Language tag column in the list of language/region names supported by Windows. Culture names follow the standard defined by BCP 47.

In most cases, the culture parameter is mapped to the corresponding National Language Support (NLS) locale identifier. The value of the culture parameter becomes the value of the LCID property of the new CultureInfo.

We recommend that you call the locale name constructor CultureInfo.CultureInfo, because locale names are preferable to LCIDs. For custom locales, a locale name is required.

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture.

Applications should typically not disallow user overrides. Disallowing overrides does not itself guarantee data stability. For more information, see the blog entry Culture data shouldn't be considered stable (except for Invariant).

If the UseUserOverride property is set to true and the specified culture identifier matches the identifier of the current Windows culture, this constructor creates a CultureInfo that uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. If the user settings are incompatible with the culture associated with the CultureInfo, for example, if the selected calendar is not one of the OptionalCalendars, the results of the methods and the values of the properties are undefined.

Otherwise, this constructor creates a CultureInfo that uses the default values for the specified culture.

The value of the useUserOverride parameter becomes the value of the UseUserOverride property.

For example, suppose that Arabic (Saudi Arabia) is the current culture of Windows and the user has changed the calendar from Hijri to Gregorian.

For cultures that use the euro, the .NET Framework and Windows XP set the default currency as euro. However, older versions of Windows do not. Therefore, if the user of an older version of Windows has not changed the currency setting through the regional and language options portion of Control Panel, the currency might be incorrect. To use the .NET Framework default setting for the currency, the application should set the useUserOverride parameter to false.

Note

For backwards compatibility, a culture constructed using a culture parameter of 0x0004 or 0x7c04 will have a Name property of zh-CHS or zh-CHT, respectively. You should instead prefer to construct the culture using the current standard culture names of zh-Hans or zh-Hant, unless you have a reason for using the older names.

Notes to Callers

.NET Framework 3.5 and earlier versions throw an ArgumentException if culture is not a valid culture identifier. Starting with .NET Framework 4, this constructor throws a CultureNotFoundException. Starting with apps that run under .NET Framework 4 or later on Windows 7 or later, the method attempts to retrieve a CultureInfo object whose identifier is culture from the operating system; if the operating system does not support that culture, the method throws a CultureNotFoundException exception.

On .NET 6 and later versions, a CultureNotFoundException is thrown if the app is running in an environment where globalization-invariant mode is enabled, for example, some Docker containers, and a culture other than the invariant culture is specified.

See also

Applies to

CultureInfo(String, Boolean)

Source:
CultureInfo.cs
Source:
CultureInfo.cs
Source:
CultureInfo.cs

Initializes a new instance of the CultureInfo class based on the culture specified by name and on a value that specifies whether to use the user-selected culture settings from Windows.

public:
 CultureInfo(System::String ^ name, bool useUserOverride);
public CultureInfo (string name, bool useUserOverride);
new System.Globalization.CultureInfo : string * bool -> System.Globalization.CultureInfo
Public Sub New (name As String, useUserOverride As Boolean)

Parameters

name
String

A predefined CultureInfo name, Name of an existing CultureInfo, or Windows-only culture name. name is not case-sensitive.

useUserOverride
Boolean

true to use the user-selected culture settings (Windows only); false to use the default culture settings.

Exceptions

name is null.

name is not a valid culture name. See the Notes to Callers section for more information.

Remarks

For a list of predefined culture names, see the Language tag column in the list of language/region names supported by Windows. Culture names follow the standard defined by BCP 47. In addition, starting with Windows 10, name can be any valid BCP-47 language tag.

If name is String.Empty, the constructor creates an instance of the invariant culture; this is equivalent to retrieving the value of the InvariantCulture property.

The user might choose to override some of the values associated with the current Windows culture through the regional and language options portion of Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture.

Applications should typically not disallow user overrides. Disallowing overrides does not itself guarantee data stability. For more information, see the blog entry Culture data shouldn't be considered stable (except for Invariant).

If the UseUserOverride property is set to true and the culture identifier associated with the specified culture name matches the culture identifier of the current Windows culture, this constructor creates a CultureInfo that uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. If the user settings are incompatible with the culture associated with the CultureInfo, for example, if the selected calendar is not one of the OptionalCalendars, the results of the methods and the values of the properties are undefined.

Otherwise, this constructor creates a CultureInfo that uses the default values for the specified culture.

The value of the useUserOverride parameter becomes the value of the UseUserOverride property.

For example, suppose that Arabic (Saudi Arabia) is the current culture of Windows and the user changed the calendar from Hijri to Gregorian.

The LCID property of the new CultureInfo is set to the culture identifier associated with the specified name.

For cultures that use the euro, the .NET Framework and Windows XP set the default currency as euro. However, older versions of Windows do not do this. Therefore, if the user of an older version of Windows has not changed the currency setting through the regional and language options portion of Control Panel, the currency might be incorrect. To use the .NET Framework default setting for the currency, the application should set the useUserOverride parameter to false.

Notes to Callers

.NET Framework 3.5 and earlier versions throw an ArgumentException if name is not a valid culture name. Starting with .NET Framework 4, this constructor throws a CultureNotFoundException. Starting with apps that run under .NET Framework 4 or later on Windows 7 or later, the method attempts to retrieve a CultureInfo object whose identifier is name from the operating system; if the operating system does not support that culture, and if name is not the name of a supplementary or replacement culture, the method throws a CultureNotFoundException exception.

On .NET 6 and later versions, a CultureNotFoundException is thrown if the app is running in an environment where globalization-invariant mode is enabled, for example, some Docker containers, and a culture other than the invariant culture is specified.

See also

Applies to