.NET
Country Codes
Programming
Code Conversion
Software Development

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:

csharp
1using System;
2using System.Globalization;
3
4var us = new RegionInfo("US");
5
6Console.WriteLine(us.TwoLetterISORegionName);   // US
7Console.WriteLine(us.ThreeLetterISORegionName); // USA
8Console.WriteLine(us.EnglishName);              // United States

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:

csharp
1using System;
2using System.Globalization;
3
4static string ToAlpha3(string alpha2)
5{
6    var region = new RegionInfo(alpha2);
7    return region.ThreeLetterISORegionName;
8}
9
10Console.WriteLine(ToAlpha3("CA")); // CAN
11Console.WriteLine(ToAlpha3("DE")); // DEU

This works well for common country-code conversion paths.

Be Ready for Invalid Input

If the code might be invalid, wrap the conversion safely.

csharp
1using System;
2using System.Globalization;
3
4static bool TryToAlpha3(string alpha2, out string alpha3)
5{
6    try
7    {
8        alpha3 = new RegionInfo(alpha2).ThreeLetterISORegionName;
9        return true;
10    }
11    catch (ArgumentException)
12    {
13        alpha3 = "";
14        return false;
15    }
16}
17
18Console.WriteLine(TryToAlpha3("FR", out var result));
19Console.WriteLine(result);

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.

csharp
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5
6static Dictionary<string, string> BuildAlpha2ToAlpha3Map()
7{
8    return CultureInfo
9        .GetCultures(CultureTypes.SpecificCultures)
10        .Select(c => new RegionInfo(c.Name))
11        .GroupBy(r => r.TwoLetterISORegionName)
12        .ToDictionary(
13            g => g.Key,
14            g => g.First().ThreeLetterISORegionName,
15            StringComparer.OrdinalIgnoreCase
16        );
17}
18
19var map = BuildAlpha2ToAlpha3Map();
20Console.WriteLine(map["JP"]); // JPN

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.

csharp
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5
6static Dictionary<string, string> BuildAlpha3ToAlpha2Map()
7{
8    return CultureInfo
9        .GetCultures(CultureTypes.SpecificCultures)
10        .Select(c => new RegionInfo(c.Name))
11        .GroupBy(r => r.ThreeLetterISORegionName)
12        .ToDictionary(
13            g => g.Key,
14            g => g.First().TwoLetterISORegionName,
15            StringComparer.OrdinalIgnoreCase
16        );
17}
18
19var reverseMap = BuildAlpha3ToAlpha2Map();
20Console.WriteLine(reverseMap["AUS"]); // AU

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.

csharp
var code = "us".ToUpperInvariant();
var region = new RegionInfo(code);
Console.WriteLine(region.EnglishName);

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

  • 'RegionInfo is 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.

Course illustration
Course illustration

All Rights Reserved.