UnM49Extensions Class
Definition
Namespace: DataStandardizer.Geography
Extension methods that read the metadata attached to the UN M49 area code enums, plus helpers that gather all related M49 codes for an enum type.
public static class UnM49Extensions
Remarks
Each metadata accessor exists as an overload extending UnM49AreaByAlpha2CountryCode, UnM49AreaByAlpha3CountryCode, and UnM49Area, returning the requested value or null when it is unavailable or the code is undefined. The name accessors take an ISO 639 language code accepting the Part 1 alpha-2 or Part 2 alpha-3 form for English, Chinese, Russian, French, Spanish, and Arabic.
A code’s attribute carries the codes and the names of every level down to and including the level the code itself occupies, so the ancestor accessors resolve from any code. GetName returns the name of whichever level the code occupies, which matters for UnM49Area because its members span every level of the hierarchy; for the two country-keyed enumerations it is equivalent to GetCountryOrAreaName.
GetIso3166Part1Alpha2Code and GetIso3166Part1Alpha3Code return the ISO 3166-1 country codes of a country or area, so a member of one country-keyed enumeration gives the code for the other, and a UnM49Area member gives both. Only the country or area level bears a country code; the world, regions, sub-regions, and intermediate regions return null.
GetLevel, GetParent, and IsWithin extend UnM49Area only, as the two country-keyed enumerations cannot represent a region as a value.
On .NET Standard targets the string? return and parameter types are string annotated with System.Diagnostics.CodeAnalysis attributes such as [MaybeNull] and [DisallowNull]. A project gets null warnings from them only on C# 8 or later with nullable enabled.
Methods
Implicit implementation
The GetM49Codes helpers are ordinary static methods (no this parameter), not extension methods.
| Method | Returns | Notes |
|---|---|---|
GetM49Codes(Type enumType) | ushort[] | All M49 codes (global, region, sub-region, intermediate, and area) for a UN M49 enum type. Throws ArgumentNullException if enumType is null; ArgumentException if it is not an enum, its underlying type is not ushort, or it has no members or a member without UnM49AreaCodeAttribute. |
GetM49Codes<T>() (where T : struct, Enum) | ushort[] | Generic form of GetM49Codes. Throws ArgumentException if the underlying type of T is not ushort, or T has no members or a member without UnM49AreaCodeAttribute. |
Extension
| Method | Extends | Returns | Notes |
|---|---|---|---|
GetCountryOrAreaName(string languageCode) | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | Country or area name for the M49 code. Returns null for a UnM49Area code above the country or area level. |
GetGlobalCode() | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | ushort? | Global code related to the M49 code. |
GetGlobalName(string languageCode) | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | Name of the global code. |
GetIntermediateRegionCode() | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | ushort? | Intermediate region code related to the M49 code. |
GetIntermediateRegionName(string languageCode) | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | Name of the intermediate region code. |
GetIso3166Part1Alpha2Code() | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | ISO 3166-1 alpha-2 country code related to the M49 code. Returns null for a UnM49Area code above the country or area level. |
GetIso3166Part1Alpha3Code() | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | ISO 3166-1 alpha-3 country code related to the M49 code. Returns null for a UnM49Area code above the country or area level. |
GetLevel() | UnM49Area | UnM49AreaLevel? | Level of the M49 hierarchy occupied by the code. |
GetName(string languageCode) | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | Name of the area at whatever level of the hierarchy it occupies. |
GetParent() | UnM49Area | UnM49Area? | Parent area in the M49 hierarchy; null for the world. |
GetRegionCode() | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | ushort? | Region code related to the M49 code. |
GetRegionName(string languageCode) | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | Name of the region code. |
GetSubRegionCode() | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | ushort? | Sub-region code related to the M49 code. |
GetSubRegionName(string languageCode) | UnM49AreaByAlpha2CountryCode / UnM49AreaByAlpha3CountryCode / UnM49Area | string? | Name of the sub-region code. |
IsWithin(UnM49Area other) | UnM49Area | bool | Whether the code falls within another area. An area does not fall within itself. |
The languageCode parameter takes an ISO 639-1 or ISO 639-2/T code, such as en or eng, and matches it without regard to the case of its letters (RFC 5646 §2.1.1), so EN and Eng give the same name as en. A bibliographic ISO 639-2/B code, such as fre or chi, isn’t matched, and a null code gives null. UN M49 gives names in Arabic, Chinese, English, French, Russian and Spanish; any other code gives null.
Applies to
Targets netstandard1.0, netstandard2.0, and net10.0.