แก้ไข

แชร์ผ่าน


RegionInfo Constructors

Definition

Initializes a new instance of the RegionInfo class.

Overloads

RegionInfo(Int32)

Initializes a new instance of the RegionInfo class based on the country/region associated with the specified culture identifier.

RegionInfo(String)

Initializes a new instance of the RegionInfo class based on the country/region or specific culture, specified by name.

RegionInfo(Int32)

Source:
RegionInfo.cs
Source:
RegionInfo.cs
Source:
RegionInfo.cs

Initializes a new instance of the RegionInfo class based on the country/region associated with the specified culture identifier.

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

Parameters

culture
Int32

A culture identifier.

Exceptions

culture specifies either an invariant, custom, or neutral culture.

-or-

.NET 6+ only: Globalization-invariant mode is enabled in the environment.

Examples

The following code example compares two instances of RegionInfo that were created differently.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Creates a RegionInfo using the ISO 3166 two-letter code.
   RegionInfo^ myRI1 = gcnew RegionInfo( "US" );
   
   // Creates a RegionInfo using a CultureInfo.LCID.
   RegionInfo^ myRI2 = gcnew RegionInfo( (gcnew CultureInfo( "en-US",false ))->LCID );
   
   // Compares the two instances.
   if ( myRI1->Equals( myRI2 ) )
      Console::WriteLine( "The two RegionInfo instances are equal." );
   else
      Console::WriteLine( "The two RegionInfo instances are NOT equal." );
}

/*
This code produces the following output.

The two RegionInfo instances are equal.

*/
using System;
using System.Globalization;

public class SamplesRegionInfo  {

   public static void Main()  {

      // Creates a RegionInfo using the ISO 3166 two-letter code.
      RegionInfo myRI1 = new RegionInfo( "US" );

      // Creates a RegionInfo using a CultureInfo.LCID.
      RegionInfo myRI2 = new RegionInfo( new CultureInfo("en-US",false).LCID );

      // Compares the two instances.
      if ( myRI1.Equals( myRI2 ) )
         Console.WriteLine( "The two RegionInfo instances are equal." );
      else
         Console.WriteLine( "The two RegionInfo instances are NOT equal." );
   }
}

/*
This code produces the following output.

The two RegionInfo instances are equal.

*/
Imports System.Globalization


Public Class SamplesRegionInfo   

   Public Shared Sub Main()

      ' Creates a RegionInfo using the ISO 3166 two-letter code.
      Dim myRI1 As New RegionInfo("US")

      ' Creates a RegionInfo using a CultureInfo.LCID.
      Dim myRI2 As New RegionInfo(New CultureInfo("en-US", False).LCID)

      ' Compares the two instances.
      If myRI1.Equals(myRI2) Then
         Console.WriteLine("The two RegionInfo instances are equal.")
      Else
         Console.WriteLine("The two RegionInfo instances are NOT equal.")
      End If 

   End Sub

End Class


'This code produces the following output.
'
'The two RegionInfo instances are equal.

Remarks

RegionInfo(String) is the recommended constructor for instantiating a RegionInfo object. Unlike the RegionInfo(Int32) constructor that requires a numeric culture identifier as a parameter, its parameter is a more readable culture name or country code.

The culture identifier is mapped to the corresponding National Language Support (NLS) locale identifier. For more information, see Windows LCID reference.

The value of the Name property of the new RegionInfo object instantiated by calling this constructor is the ISO 3166 2-letter code for the country/region, not the combined language and country/region code. For example, if a RegionInfo object is instantiated with the culture identifier 0x0409 for the English (United States) culture, the value of the Name property is "US". In contrast, if a RegionInfo object is instantiated with the combined language and country/region code en-US for the English (United States) culture, the value of the Name property is "en-US" in .NET Framework and just "US" in .NET Core and .NET 5+.

See also

Applies to

RegionInfo(String)

Source:
RegionInfo.cs
Source:
RegionInfo.cs
Source:
RegionInfo.cs

Initializes a new instance of the RegionInfo class based on the country/region or specific culture, specified by name.

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

Parameters

name
String

A string that contains a two-letter code defined in ISO 3166 for country/region.

-or-

A string that contains the culture name for a specific culture, custom culture, or Windows-only culture. If the culture name is not in RFC 4646 format, your application should specify the entire culture name instead of just the country/region.

Exceptions

name is null.

name is not a valid country/region name or specific culture name.

-or-

.NET 6+ only: Globalization-invariant mode is enabled in the environment.

Examples

The following code example compares two instances of RegionInfo that were created differently.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Creates a RegionInfo using the ISO 3166 two-letter code.
   RegionInfo^ myRI1 = gcnew RegionInfo( "US" );
   
   // Creates a RegionInfo using a CultureInfo.LCID.
   RegionInfo^ myRI2 = gcnew RegionInfo( (gcnew CultureInfo( "en-US",false ))->LCID );
   
   // Compares the two instances.
   if ( myRI1->Equals( myRI2 ) )
      Console::WriteLine( "The two RegionInfo instances are equal." );
   else
      Console::WriteLine( "The two RegionInfo instances are NOT equal." );
}

/*
This code produces the following output.

The two RegionInfo instances are equal.

*/
using System;
using System.Globalization;

public class SamplesRegionInfo  {

   public static void Main()  {

      // Creates a RegionInfo using the ISO 3166 two-letter code.
      RegionInfo myRI1 = new RegionInfo( "US" );

      // Creates a RegionInfo using a CultureInfo.LCID.
      RegionInfo myRI2 = new RegionInfo( new CultureInfo("en-US",false).LCID );

      // Compares the two instances.
      if ( myRI1.Equals( myRI2 ) )
         Console.WriteLine( "The two RegionInfo instances are equal." );
      else
         Console.WriteLine( "The two RegionInfo instances are NOT equal." );
   }
}

/*
This code produces the following output.

The two RegionInfo instances are equal.

*/
Imports System.Globalization


Public Class SamplesRegionInfo   

   Public Shared Sub Main()

      ' Creates a RegionInfo using the ISO 3166 two-letter code.
      Dim myRI1 As New RegionInfo("US")

      ' Creates a RegionInfo using a CultureInfo.LCID.
      Dim myRI2 As New RegionInfo(New CultureInfo("en-US", False).LCID)

      ' Compares the two instances.
      If myRI1.Equals(myRI2) Then
         Console.WriteLine("The two RegionInfo instances are equal.")
      Else
         Console.WriteLine("The two RegionInfo instances are NOT equal.")
      End If 

   End Sub

End Class


'This code produces the following output.
'
'The two RegionInfo instances are equal.

The following code example creates instances of RegionInfo using culture names.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

namespace Sample
{
    public ref class SamplesRegionInfo  
    {
    public: 
        static void Work()  
        {

            // Creates an array containing culture names.
            array <String^>^ commonCultures = 
                {"", "ar", "ar-DZ", "en", "en-US"};

            // Creates a RegionInfo for each of the culture names.
            // Note that "ar" is the culture name for the neutral 
            // culture  "Arabic", but it is also the region name for
            // the country/region "Argentina"; therefore, it does not 
            // fail as expected.
            Console::WriteLine("Without checks...");
            for each (String^ cultureID in commonCultures)
            {   
                try  
                {
                    RegionInfo^ region = 
                    gcnew RegionInfo(cultureID);
                }

                catch (ArgumentException^ ex)  
                {
                    Console::WriteLine(ex);
                }
            }

            Console::WriteLine();

            Console::WriteLine("Checking the culture"
                " names first...");

            for each (String^ cultureID in commonCultures)
            {
                if (cultureID->Length == 0)  
                {
                    Console::WriteLine(
                        "The culture is the invariant culture.");
                }
                else  
                {
                    CultureInfo^ culture = 
                        gcnew CultureInfo(cultureID, false);
                    if (culture->IsNeutralCulture)
                    {
                        Console::WriteLine("The culture {0} is "
                            "a neutral culture.", cultureID);
                    }
                    else   
                    {
                        Console::WriteLine("The culture {0} is "
                            "a specific culture.", cultureID);
                        try  
                        {
                            RegionInfo^ region = 
                                gcnew RegionInfo(cultureID);
                        }
                        catch (ArgumentException^ ex)  
                        {
                            Console::WriteLine(ex);
                        }
                    }
                }
            } 
            Console::ReadLine();
        }
    };
}

int main()
{
    Sample::SamplesRegionInfo::Work();
    return 0;
}

/*
This code produces the following output.

Without checks...
System.ArgumentException: Region name '' is not supported.
Parameter name: name
at System.Globalization.RegionInfo..ctor(String name)
at SamplesRegionInfo.Main()
System.ArgumentException: Region name 'en' is not supported.
Parameter name: name
at System.Globalization.CultureTableRecord..ctor(String regionName, 
   Boolean useUserOverride)
at System.Globalization.RegionInfo..ctor(String name)
at SamplesRegionInfo.Main()

Checking the culture names first...
The culture is the invariant culture.
The culture ar is a neutral culture.
The culture ar-DZ is a specific culture.
The culture en is a neutral culture.
The culture en-US is a specific culture.

*/
using System;
using System.Globalization;

public class SamplesRegionInfo  {

   public static void Main()  {

      // Creates an array containing culture names.
      String[] myCultures = new String[]  { "", "ar", "ar-DZ", "en", "en-US" };

      // Creates a RegionInfo for each of the culture names.
      //    Note that "ar" is the culture name for the neutral culture "Arabic",
      //    but it is also the region name for the country/region "Argentina";
      //    therefore, it does not fail as expected.
      Console.WriteLine("Without checks...");
      foreach (String culture in myCultures)  {
         try  {
            RegionInfo myRI = new RegionInfo( culture );
         }
         catch ( ArgumentException e )  {
            Console.WriteLine( e.ToString() );
         }
      }

      Console.WriteLine();

      Console.WriteLine( "Checking the culture names first..." );
      foreach (String culture in myCultures)  {
         if ( culture == "" )  {
            Console.WriteLine("The culture is the invariant culture.");
         }
         else  {
            CultureInfo myCI = new CultureInfo( culture, false );
            if ( myCI.IsNeutralCulture )
                {
                    Console.WriteLine( "The culture {0} is a neutral culture.", culture );
                }
                else   {
               Console.WriteLine( "The culture {0} is a specific culture.", culture );
               try  {
                  RegionInfo myRI = new RegionInfo( culture );
               }
               catch ( ArgumentException e )  {
                  Console.WriteLine( e.ToString() );
               }
            }
         }
      }
   }
}

/*
This code produces the following output.

Without checks...
System.ArgumentException: Region name '' is not supported.
Parameter name: name
   at System.Globalization.RegionInfo..ctor(String name)
   at SamplesRegionInfo.Main()
System.ArgumentException: Region name 'en' is not supported.
Parameter name: name
   at System.Globalization.CultureTableRecord..ctor(String regionName, Boolean useUserOverride)
   at System.Globalization.RegionInfo..ctor(String name)
   at SamplesRegionInfo.Main()

Checking the culture names first...
The culture is the invariant culture.
The culture ar is a neutral culture.
The culture ar-DZ is a specific culture.
The culture en is a neutral culture.
The culture en-US is a specific culture.

*/
Imports System.Globalization

Public Class SamplesRegionInfo

    Public Shared Sub Main()

        ' Creates an array containing culture names.
        Dim myCultures() As String = {"", "ar", "ar-DZ", "en", "en-US"}

        Dim culture As String

        ' Creates a RegionInfo for each of the culture names.
        '    Note that "ar" is the culture name for the neutral culture "Arabic",
        '    but it is also the region name for the country/region "Argentina";
        '    therefore, it does not fail as expected.
        Console.WriteLine("Without checks...")
        For Each culture In  myCultures
            Try
                Dim myRI As New RegionInfo(culture)
            Catch e As ArgumentException
                Console.WriteLine(e.ToString())
            End Try
        Next

        Console.WriteLine()

        Console.WriteLine("Checking the culture names first...")
        For Each culture In  myCultures
            If culture = "" Then
                Console.WriteLine("The culture is the invariant culture.")
            Else
                Dim myCI As New CultureInfo(culture, False)
                If myCI.IsNeutralCulture Then
                    Console.WriteLine("The culture {0} is a neutral culture.", culture)
                Else
                    Console.WriteLine("The culture {0} is a specific culture.", culture)
                    Try
                        Dim myRI As New RegionInfo(culture)
                    Catch e As ArgumentException
                        Console.WriteLine(e.ToString())
                    End Try
                End If
            End If
        Next
    End Sub  
End Class 
'The example displays the following output.
'
'Without checks...
'System.ArgumentException: Region name '' is not supported.
'Parameter name: name
'   at System.Globalization.RegionInfo..ctor(String name)
'   at SamplesRegionInfo.Main()
'System.ArgumentException: Region name 'en' is not supported.
'Parameter name: name
'   at System.Globalization.CultureTableRecord..ctor(String regionName, Boolean useUserOverride)
'   at System.Globalization.RegionInfo..ctor(String name)
'   at SamplesRegionInfo.Main()
'
'Checking the culture names first...
'The culture is the invariant culture.
'The culture ar is a neutral culture.
'The culture ar-DZ is a specific culture.
'The culture en is a neutral culture.
'The culture en-US is a specific culture.

Remarks

The name parameter is either one of the codes defined for country/region, or a specific, custom, or Windows-only culture name. Case is not significant. However, the Name, TwoLetterISORegionName, and ThreeLetterISORegionName properties return the specified code or culture name in uppercase.

The predefined RegionInfo names are listed in ISO 3166: Country codes.

You should provide the name of a specific culture rather than just a country/region name in the name parameter. For example, en-US for English (United States) or es-US for Spanish (United States) is preferable to US because properties such as NativeName and CurrencyNativeName reflect a specific language. az-Latn-AZ or az-Cyrl-AZ is preferable to AZ because properties such as NativeName, CurrencyNativeName, and CurrencySymbol reflect a specific script. The predefined culture names are listed at Windows LCID reference.

This constructor throws an ArgumentException if name is a neutral culture (such as en for English).

Notes to Callers

This constructor accepts only specific cultures or country/region codes. However, some neutral culture names are the same as country/region codes. In this case, name is interpreted as a country/region code rather than a neutral culture name, and this constructor does not throw an ArgumentException.

Applies to