Converting country codes in .NET
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting country codes in .NET usually means translating between formats such as ISO 3166-1 alpha-2 and alpha-3. .NET gives you RegionInfo, which exposes some useful country metadata, but it does not provide a one-line “convert every country code format to every other format” API. In practice, the most reliable approach is to build a mapping from RegionInfo data and be explicit about which code systems you support.
What .NET Gives You
System.Globalization.RegionInfo exposes properties such as:
- '
TwoLetterISORegionName' - '
ThreeLetterISORegionName' - '
EnglishName' - '
NativeName'
Example:
If your input is already a valid region identifier such as "US", RegionInfo can give you the other common representations easily.
Direct Alpha-2 to Alpha-3 Conversion
If you trust the input to be a valid alpha-2 code, the simplest conversion is:
This works well for common country-code conversion paths.
Be Ready for Invalid Input
If the code might be invalid, wrap the conversion safely.
That is better than letting random user or file input crash the conversion path.
Build a Reusable Mapping Table
If you need repeated conversions in both directions, build a dictionary once rather than constructing RegionInfo objects ad hoc all over the codebase.
The grouping step matters because multiple cultures can map to the same region.
Reverse Conversion: Alpha-3 to Alpha-2
RegionInfo can expose alpha-3 codes, but it is not as convenient for reverse lookup from arbitrary alpha-3 input. A dictionary is usually the cleanest answer.
For bulk conversions, this pattern is clearer and faster than repeated trial construction.
Watch Out for Code System Confusion
Country-code discussions often mix:
- ISO alpha-2
- ISO alpha-3
- ISO numeric
- Windows region naming
- locale or culture names such as
en-US
These are not interchangeable. For example, en-US is a culture, not a country code. If you accept several formats, detect and normalize them explicitly rather than treating all short strings as the same thing.
When You Need Numeric ISO Codes
RegionInfo is most useful for the alphabetic ISO region names. If you need ISO numeric country codes, you may need an external mapping or your own reference table, because that is not the strongest built-in .NET conversion path.
This is a good example of why the exact input and output formats should be defined early.
Normalize Input Consistently
Country codes are usually case-insensitive in application logic, so normalize them or use case-insensitive dictionaries.
This avoids small input-format differences causing unnecessary lookup failures.
Common Pitfalls
The biggest mistake is confusing culture names like en-US with plain country codes like US. Another is assuming RegionInfo gives effortless conversion among every country-code system when it mainly helps with the common alphabetic ISO region names. Developers also often forget to deduplicate regions when building lookup tables from specific cultures. Finally, reverse lookup from alpha-3 usually works best with a dictionary rather than by hoping RegionInfo accepts every format directly.
Summary
- '
RegionInfois the main built-in .NET type for common country metadata.' - Alpha-2 to alpha-3 conversion is straightforward with
RegionInfo. - For repeated or reverse lookups, build explicit dictionaries.
- Be clear about which code system you are converting from and to.
- Do not confuse cultures, country codes, and other region-related identifiers.

