Recursive AI

A libphonenumber C# alternative: OneContact

Replace libphonenumber-csharp parsing, validation, and formatting with OneContact. See both packages run and learn what to regression-test.

OneContact uses an immutable PhoneNumber instead of PhoneNumberUtil.

OneContact is a free, open-source contact-data library for .NET from Recursive AI, licensed MIT OR Apache-2.0. NuGet · GitHub.

InstallNuGet
dotnet add package RecursiveAI.OneContact --version 1.0.0
Program.cs · OneContact.NET 10
using RecursiveAI.OneContact;
using RecursiveAI.OneContact.Phones;
 
foreach (var (text, region) in new (string, Region?)[]
{
    ("(415) 555-2671", Region.US),
    ("+1 415 555 2671", null),
    ("+1 123 456 7890", null),
    ("not a number", Region.US),
})
{
    try
    {
        var number = region is Region r
            ? PhoneNumber.Parse(text, r)
            : PhoneNumber.Parse(text);
        Console.WriteLine($"{number.IsValid} | " +
            $"{number.Format(PhoneFormat.E164)} | " +
            $"{number.Format(PhoneFormat.National)}");
    }
    catch (ContactParseException e)
    {
        Console.WriteLine($"Parse failed: {e.Reason}");
    }
}

Run the OneContact example above in a .NET 10 console project. It prints validity, E.164, and national format for national and international US input, then tests an invalid number and unreadable text.

OutputOneContact 1.0.0
True | +14155552671 | (415) 555-2671
True | +14155552671 | (415) 555-2671
False | +11234567890 | 1234567890
Parse failed: phone.not-a-number

Before: libphonenumber-csharp 9.0.44

Run this version in a separate .NET 10 console project. The port’s NuGet README documents PhoneNumberUtil.GetInstance(), region strings for national input, and null for +-prefixed input.

InstallNuGet
dotnet add package libphonenumber-csharp --version 9.0.44
Program.cs · libphonenumber-csharp.NET 10
using PhoneNumbers;
 
var util = PhoneNumberUtil.GetInstance();
foreach (var (text, region) in new (string, string?)[]
{
    ("(415) 555-2671", "US"),
    ("+1 415 555 2671", null),
    ("+1 123 456 7890", null),
    ("not a number", "US"),
})
{
    try
    {
        var number = util.Parse(text, region);
        Console.WriteLine($"{util.IsValidNumber(number)} | " +
            $"{util.Format(number, PhoneNumberFormat.E164)} | " +
            $"{util.Format(number, PhoneNumberFormat.NATIONAL)}");
    }
    catch (NumberParseException e)
    {
        Console.WriteLine($"Parse failed: {e.ErrorType}");
    }
}
Outputlibphonenumber-csharp 9.0.44
True | +14155552671 | (415) 555-2671
True | +14155552671 | (415) 555-2671
False | +11234567890 | 1234567890
Parse failed: NOT_A_NUMBER

Call mapping

These mappings use the port’s API docs and the OneContact migration reference. number means the result from that column’s parser.

Tasklibphonenumber-csharpOneContact
Parse US national inpututil.Parse(text, "US")PhoneNumber.Parse(text, Region.US)
Parse international input starting with +util.Parse(text, null)PhoneNumber.Parse(text)
Check numbering-plan validityutil.IsValidNumber(number)number.IsValid
Format for storageutil.Format(number, PhoneNumberFormat.E164)number.Format(PhoneFormat.E164)
Format for local displayutil.Format(number, PhoneNumberFormat.NATIONAL)number.Format(PhoneFormat.National)
Format for international displayutil.Format(number, PhoneNumberFormat.INTERNATIONAL)number.Format(PhoneFormat.International)
Read a parse failureNumberParseException.ErrorTypeContactParseException.Reason; translate to your own error categories

Use Region.Parse(countryCode) for an ISO region string instead of passing that string to the parser. Keep region selection at the input boundary rather than inferring it from the machine’s locale.

Regression checklist

  • National versus international: Test each supported region’s national input, its +-prefixed equivalent without a region, and national input with the region omitted. OneContact reports phone.region-required for national input with no region. Test region-specific dialing prefixes separately from + input.
  • Parse failure versus invalid number: Include unreadable text and a parseable but invalid number. In the output above, both parsers can format the invalid number; that does not make it valid, let alone reachable. Translate the two libraries’ errors at your application boundary instead of matching exception messages.
  • Formatting and stored data: Compare E.164 and display strings on your own fixtures, including extensions and leading zeros if you store them. Do not overwrite old records until you know which output changes your app can accept.
  • Metadata changes: Record both package versions with the baseline results. The port’s README describes frequent upstream metadata updates. OneContact bundles numbering-plan data from a pinned libphonenumber release and ships newer data in patch releases. Rerun your fixture set when either package changes and review validity and formatting differences rather than treating every change as a parser bug.

If your app also finds numbers in free text or formats them as users type, test those separately using the phone API reference. The agreement above covers the four inputs shown, not every operation or numbering plan.