共用方式為


RegionInfo 建構函式

定義

初始化 RegionInfo 類別的新執行個體。

多載

RegionInfo(Int32)

根據與指定文化特性識別項相關的國家/地區,初始化 RegionInfo 類別的新執行個體。

RegionInfo(String)

根據由名稱指定的國家/地區或特定文化特性,初始化 RegionInfo 類別的新執行個體。

RegionInfo(Int32)

來源:
RegionInfo.cs
來源:
RegionInfo.cs
來源:
RegionInfo.cs

根據與指定文化特性識別項相關的國家/地區,初始化 RegionInfo 類別的新執行個體。

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

參數

culture
Int32

文化特性識別項。

例外狀況

culture 指定不變、自訂或中性文化特性。

-或-

僅限 .NET 6+ :在環境中啟用全球化非變異模式。

範例

下列程式代碼範例會比較兩個以不同方式建立的 RegionInfo 實例。

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.

備註

RegionInfo(String) 是具現化 RegionInfo 對象的建議建構函式。 RegionInfo(Int32)不同於需要數值文化特性標識碼做為參數的建構函式,其參數是更容易閱讀的文化特性名稱或國家/地區代碼。

文化特性標識碼會對應到對應的國家語言支援 (NLS) 地區設定標識符。 如需詳細資訊,請參閱 Windows LCID 參考

Name呼叫這個建構函式來具現化之新RegionInfo對象的 屬性值是國家/地區的 ISO 3166 2 字母代碼,而不是合併的語言和國家/地區代碼。 例如,如果RegionInfo物件是使用英文 (美國) 文化特性的文化特性標識碼0x0409具現化,則 屬性的值Name會是 “US”。 相反地,如果RegionInfo物件是使用英文 (美國) 文化特性的合併語言和國家/地區代碼en-US具現化,則屬性的值Name在 .NET Framework 中為 “en-US”,而只有 .NET Core 和 .NET 5+ 中的 “US”。

另請參閱

適用於

RegionInfo(String)

來源:
RegionInfo.cs
來源:
RegionInfo.cs
來源:
RegionInfo.cs

根據由名稱指定的國家/地區或特定文化特性,初始化 RegionInfo 類別的新執行個體。

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

參數

name
String

字串,包含 ISO 3166 中針對國家/地區定義的兩個字母代碼。

-或-

包含特定文化特性、自訂文化特性或 Windows 專用文化特性之文化特性名稱的字串。 如果文化特性名稱的格式不是 RFC 4646,則應用程式應該會指定整個文化特性名稱,而不單是國家/地區。

例外狀況

namenull

name 不是有效的國家/地區名稱或特定文化特性名稱。

-或-

僅限 .NET 6+ :在環境中啟用全球化非變異模式。

範例

下列程式代碼範例會比較兩個以不同方式建立的 RegionInfo 實例。

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.

下列程式代碼範例會使用文化特性名稱建立的 RegionInfo 實例。

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.

備註

參數 name 是針對國家/地區或特定、自定義或僅限 Windows 文化特性名稱定義的其中一個程序代碼。 案例並不重要。 不過,、 TwoLetterISORegionNameThreeLetterISORegionName 屬性會Name以大寫傳回指定的程式代碼或文化特性名稱。

預先定義 RegionInfo 的名稱會列在 ISO 3166:國家/地區代碼中。

您應該提供特定文化特性的名稱,而不只是參數中的 name 國家/地區名稱。 例如,en-US對於英文 (美國) 或es-US西班牙文 (美國) ,最好US是因為 和 等NativeNameCurrencyNativeName屬性會反映特定語言。 az-Latn-AZaz-Cyrl-AZ 最好是 AZ ,因為 、 CurrencyNativeNameCurrencySymbolNativeName屬性會反映特定的腳本。 預先定義的文化特性名稱列在 Windows LCID 參考中。

如果 name 是中性文化特性 (,例如en英文) ,這個建構函式會ArgumentException擲回 。

給呼叫者的注意事項

此建構函式只接受特定文化特性或國家/地區代碼。 不過,某些中性文化特性名稱與國家/地區代碼相同。 在這裡情況下, name 會解譯為國家/地區代碼,而不是中性文化特性名稱,而且此建構函式不會擲回 ArgumentException

適用於