This documentation is archived and is not being maintained.

Using the CultureInfo Class

The CultureInfo class contains culture-specific information, such as the language, country/region, calendar, and cultural conventions. This class also provides the information required for performing culture-specific operations, such as casing, formatting dates and numbers, and comparing strings.

The CultureInfo class specifies a unique name for each culture. For a list of culture names, see the description of the CultureInfo class. Your application can use the GetCultures method to retrieve a complete list of all cultures. The following example displays a list of all cultures.

Imports System.Globalization

Public Class Example
   Public Shared Sub Main()  
      For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.AllCultures)
   End Sub
End Class


.NET Framework applications can access unmanaged functions in dynamic link libraries using the A Closer Look at Platform Invoke service.

To initialize country/region information, your application can pass a CultureInfo object to a RegionInfo object that corresponds to the country/region. Alternatively, in unmanaged code, the application can pass a CultureInfo object to the Win32 function GetLocaleInfo.

To initialize a RegionInfo object using a CultureInfo object, your application must specify a CultureInfo object that represents a specific culture, such as Arabic (Algeria), named "ar-DZ". An attempt to initialize a RegionInfo object with a CultureInfo object that represents a neutral culture, such as Arabic ("ar"), throws an exception. The neutral culture does not specify the country or region information necessary for mapping to a country/region.

The GetLocaleInfo method differs from the RegionInfo constructor in that it returns a country/region for a CultureInfo object that represents either a specific culture or a neutral culture. For example, if the application passes a CultureInfo object that represents the neutral culture Arabic to GetLocaleInfo, the method maps the neutral culture to the default country/region with which it is associated. In this case, GetLocaleInfo retrieves Saudi Arabia. Be careful when using the GetLocaleInfo method, as the default country/region mapping it provides might be culturally inappropriate for your application. To eliminate this discrepancy, have your application use only specific cultures when interoperating with an API function.

The following example demonstrates how the RegionInfo class constructor and the GetLocaleInfo method can retrieve different values for the same CultureInfo object. Note that when the CultureInfo object represents the specific culture Arabic (Algeria), both methods retrieve Algeria as the country/region. However, when the CultureInfo object represents the neutral culture Arabic, the results differ. The RegionInfo constructor fails to retrieve a country/region, while GetLocaleInfo retrieves Saudi Arabia.

Imports System.Globalization
Imports System.Runtime.InteropServices

Module CountryRegionName
   ' The name of a country or region in English.
   Private LOCALE_SENGCOUNTRY As Integer = &H1002

   ' Use COM interop to call the Win32 API GetLocalInfo.
   Declare Unicode Function GetLocaleInfoW Lib "Kernel32.dll" _
      (Locale As Integer, LCType As Integer,<[In](), _
       MarshalAs(UnmanagedType.LPWStr)> lpLCData As String, _
       cchData As Integer) As Integer

   ' A method to retrieve the .NET Framework Country/Region
   ' that maps to the specified CultureInfo.
   Public Function GetNetCountryRegionName(ci As CultureInfo) As String
      ' If the specified CultureInfo represents a specific culture,
      ' the attempt to create a RegionInfo succeeds.
         Dim ri As New RegionInfo(ci.LCID)
         Return ri.EnglishName
         ' Otherwise, the specified CultureInfo represents a neutral
         'culture, and the attempt to create a RegionInfo fails.
         Return String.Empty
      End Try
   End Function

   ' A method to retrieve the Win32 API Country/Region
   ' that maps to the specified CultureInfo.
   Public Function GetWinCountryRegionName(ci As CultureInfo) As String
      Dim size As Integer = GetLocaleInfoW(ci.LCID, _
         LOCALE_SENGCOUNTRY, Nothing, 0)
      Dim str As New String(" "c, size)
      Dim err As Integer = GetLocaleInfoW(ci.LCID, _
         LOCALE_SENGCOUNTRY, str, size)
      ' If the string is not empty, GetLocaleInfoW succeeded.
      ' It will succeed regardless of whether ci represents
      ' a neutral or specific culture.
      If err <> 0 Then
         Return str
         Return String.Empty
      End If
   End Function

   Public Sub Main()
      ' Create a CultureInfo initialized to the neutral Arabic culture.
      Dim ci1 As New CultureInfo("ar")
      Console.WriteLine(ControlChars.NewLine + _
         "The .NET Region name: {0}", _
      Console.WriteLine("The Win32 Region name: {0}", _

      ' Create a CultureInfo initialized to the specific 
      ' culture Arabic in Algeria.
      Dim ci2 As New CultureInfo("ar-DZ")
      Console.WriteLine(ControlChars.NewLine + _
         "The .NET Region name: {0}", _
      Console.WriteLine("The Win32 Region name: {0}", _
   End Sub
End Module
' The example displays the following output:
'   The .NET Region name:
'   The Win32 Region name: Saudi Arabia
'   The .NET Region name: Algeria
'   The Win32 Region name: Algeria