CultureInfo Constructor (String)
Updated: June 2010
Initializes a new instance of the CultureInfo class based on the culture specified by name.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
- Type: System.String
A predefined CultureInfo name, Name of an existing CultureInfo, or Windows-only culture name. name is not case-sensitive.
| Exception | Condition |
|---|---|
| ArgumentNullException |
name is null. |
| CultureNotFoundException |
name is not a valid culture name. |
For a list of predefined culture names, see the National Language Support (NLS) API Reference at the Go Global Developer Center.
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.
-
With CultureInfo("ar-SA"), Calendar is set to GregorianCalendar (which is the user setting) and UseUserOverride is set to true.
-
With CultureInfo("th-TH"), Calendar is set to ThaiBuddhistCalendar (which is the default calendar for th-TH) and UseUserOverride is set to true.
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 use a CultureInfo constructor overload that accepts a useUserOverride parameter and set it to false.
Notes to Callers
The .NET Framework version 3.5 and earlier versions throw an ArgumentException if name is not a valid culture name. Starting with the .NET Framework version 4, this constructor throws a CultureNotFoundException.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
In the previous example, the call to String.IsNullOrEmpty should be replaced by a test for a null string, since String.Empty is a valid culture name (the name of the invariant culture). Otherwise, this code is best used to validate culture names if only a few culture names are to be validated. If potentially a large number of culture names have to be validated, the use of exception handling to detect whether or not a culture name is valid is extremely expensive. A better alternative is to retrieve and store a list of culture names, which can then be compared with the culture name to be validated when it is needed. Here is the C# code that does that:
using System;
using System.Collections.Generic;
using System.Globalization;
public static class CultureUtilities
{
private static List<string> cultureNames;
static CultureUtilities()
{
cultureNames = new List<string>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
Array.ForEach(cultures, s => cultureNames.Add(s.Name));
}
public static bool IsValidCulture(string name)
{
if (cultureNames.Contains(name))
return true;
else
return false;
}
}
And here is the Visual Basic code:
Imports System.Collections.Generic
Imports System.Globalization
Public Module CultureUtilities
Private cultureNames As List(Of String)
Sub New()
cultureNames = New List(Of String)()
Dim cultures As CultureInfo() = CultureInfo.GetCultures(CultureTypes.AllCultures)
Array.ForEach(cultures, Sub(s) cultureNames.Add(s.Name))
End Sub
Public Function IsValidCulture(name As String) As Boolean
If cultureNames.Contains(name) Then
Return True
Else
Return False
End If
End Function
End Module
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
- 4/19/2011
- R Petrusha - MSFT
VB.NET
ImportsSystem.GlobalizationC#
PublicFunctionIsValidCulture(culture AsString)AsBoolean
IfString.IsNullOrEmpty(culture)Then
ReturnFalse
EndIf
Try
Dim ci AsNewCultureInfo(culture)
Catch ex AsCultureNotFoundException
ReturnFalse
EndTry
ReturnTrue
EndFunction
using System.Globalization;
public bool IsValidCulture(string culture)
{
if(String.IsNullOrEmpty(culture))
{
return false;
}
try
{
CultureInfo ci = new CultureInfo(culture);
}
catch(CultureNotFoundException)
{
return false;
}
return true;
}
- 4/6/2011
- rtpHarry