This topic has not yet been rated - Rate this topic

CultureInfo Constructor (String)

Updated: June 2010

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

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
public CultureInfo(
	string name
)

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.

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.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

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.

Date

History

Reason

June 2010

Updated exception information.

Content bug fix.

May 2010

Added information about initializing the invariant culture.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
An Alternative Version of the Usage Example

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



Usage Example
This is a simple usage example which shows how you can use CultureInfo to test if a string contains a valid culture. There might be a better way to do this if you are more familiar with the System.Globalization namespace but I just wrote this piece of code to answer a question over at forums.asp.net:

VB.NET
ImportsSystem.Globalization

PublicFunctionIsValidCulture(culture AsString)AsBoolean
       
IfString.IsNullOrEmpty(culture)Then
               
ReturnFalse
       
EndIf

       
Try
               
Dim ci AsNewCultureInfo(culture)
       
Catch ex AsCultureNotFoundException
               
ReturnFalse
       
EndTry

       
ReturnTrue
EndFunction
C#
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;
}