RegionInfo Constructor
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the RegionInfo class for a particular country/region based on its full culture name.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
- Type: System.String
A string containing the full culture name for a specific culture.
| Exception | Condition |
|---|---|
| ArgumentNullException | name is null. |
| ArgumentException | name is a country/region name. -or- name is the name of a neutral culture. -or- name is not a valid culture name. |
The specified name must be the name of a specific culture. It cannot be the name of a neutral culture. For example, en-US or en-ZW are valid strings to pass to the name parameter; en is not. Case is not significant. The predefined culture names are listed in the CultureInfo class topic.
The following code example compares two instances of RegionInfo that were created differently.
using System; using System.Globalization; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Creates a RegionInfo using the es-US culture name. RegionInfo myRI1 = new RegionInfo("es-US"); // Creates a RegionInfo using a CultureInfo Name property RegionInfo myRI2 = new RegionInfo(new CultureInfo("en-US").Name); // Compares the two instances. if (myRI1.Equals(myRI2)) outputBlock.Text += "The two RegionInfo instances are equal." + "\n"; else outputBlock.Text += "The two RegionInfo instances are NOT equal." + "\n"; } } /* This code produces the following output. The two RegionInfo instances are equal. */
The following code example creates instances of RegionInfo using culture names.
using System; using System.Globalization; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // 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. outputBlock.Text += "Without checks..." + "\n"; foreach (String culture in myCultures) { try { RegionInfo myRI = new RegionInfo(culture); } catch (ArgumentException e) { outputBlock.Text += e.ToString() + "\n"; } } outputBlock.Text += "\n"; outputBlock.Text += "Checking the culture names first..." + "\n"; foreach (String culture in myCultures) { if (culture == "") { outputBlock.Text += "The culture is the invariant culture." + "\n"; } else { CultureInfo myCI = new CultureInfo(culture); if (myCI.IsNeutralCulture) outputBlock.Text += String.Format("The culture '{0}' is a neutral culture.", culture) + "\n"; else { outputBlock.Text += String.Format("The culture '{0}' is a specific culture.", culture) + "\n"; try { RegionInfo myRI = new RegionInfo(culture); } catch (ArgumentException e) { outputBlock.Text += e.ToString() + "\n"; } } } } } } /* This example produces the following output. Without checks... System.ArgumentException: The region name ar should not correspond to neutral culture; a specific culture is required. Parameter name: name at System.Globalization.RegionInfo..ctor(String name) at Example.Demo(TextBlock outputBlock) System.ArgumentException: The region name ar should not correspond to neutral culture; a specific culture is required. Parameter name: name at System.Globalization.RegionInfo..ctor(String name) at Example.Demo(TextBlock outputBlock) 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. */