Offical C# port of https://github.com/googlei18n/libphonenumber
Offical C# port of https://github.com/googlei18n/libphonenumber
C# port of Google's libphonenumber library.
The code was rewritten from the Java source mostly unchanged, please refer to the original documentation for sample code and API documentation.
The original Apache License 2.0 was preserved.
[!TIP] Try the interactive demo → — parse, format, validate, and find phone numbers in your browser. No install required; runs entirely via WebAssembly.
See this for details about the port.
Phone number metadata is updated in the Google repo approximately every two weeks. This library is automatically updated by a scheduled github action to include the latest metadata, usually within a day. See Metadata updates for how that works and how to run it manually.
See CHANGELOG.md for release history.
Run the following command to add this library to your project
dotnet add package libphonenumber-csharpAvailable on NuGet as package libphonenumber-csharp.
Targets netstandard2.0, net8.0 and net10.0.
libphonenumber-csharp.extensions is an optional companion package with helpers that suit C# better than the ported Java API — PhoneNumber.TryParse and PhoneNumber.TryParseValid return a bool instead of throwing, and PhoneNumberConverter is a System.Text.Json converter for PhoneNumber.
The library is annotated as trim- and AOT-compatible, and the trim/AOT analyzers run as part of its own build. All metadata — including the geocoding, carrier and time zone prefix maps — is compiled to a binary form at build time and embedded in the assembly as compressed resources, so no XML is parsed and no file is read from disk at run time. The interactive demo is a Blazor WebAssembly app that runs this library trimmed, in the browser.
Validation and formatting are driven by regexes built from the bundled metadata. There are thousands of them, and each is built the first time some caller touches that region.
Those metadata regexes are deliberately not built with RegexOptions.Compiled. Compiling one costs around 1.5 ms of IL-emit and saves roughly 0.044 µs per match, so a pattern has to be matched on the order of 30,000 times before compiling it breaks even — which metadata patterns rarely are, because a workload spread across regions matches each one comparatively few times. Measured end-to-end on net8.0 (total wall time including startup, parse + validate + format), compiling them is 34x slower for 1,000 operations across 245 regions, 3.7x slower for 100,000, and still 1.7x slower at 1,000,000. It wins only for a process concentrating very high volume on one or two regions, and then by 6–20%.
The library's own fixed regexes are a different case and are still compiled: there is a small, fixed number of them, each built once per process and hot for its whole life.
There is no per-call knob for this. If you have measured a workload where compiling the metadata patterns wins — a long-running process concentrating very high volume on one or two regions is the shape where it can — please open an issue with the numbers rather than reaching for internal types.
Symbols are published to the NuGet.org symbol server as a .snupkg alongside each release, with Source Link wired up — enable symbol server support in your debugger to step into the library.
using PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var e164PhoneNumber = "+44 117 496 0123";
var nationalPhoneNumber = "2024561111";
var smsShortNumber = "83835";
var phoneNumber = phoneNumberUtil.Parse(e164PhoneNumber, null);
phoneNumber = phoneNumberUtil.Parse(nationalPhoneNumber, "US");
phoneNumber = phoneNumberUtil.Parse(smsShortNumber, "US");using PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", "US");
var formattedPhoneNumber = phoneNumberUtil.Format(phoneNumber, PhoneNumberFormat.INTERNATIONAL);
var formattedPhoneNumberNational = phoneNumberUtil.Format(phoneNumber, PhoneNumberFormat.NATIONAL);
Console.WriteLine(formattedPhoneNumber.ToString()); // +1 415-666-7777
Console.WriteLine(formattedPhoneNumberNational.ToString()); // (415) 666-7777using PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", "US");
var isValid = phoneNumberUtil.IsValidNumber(phoneNumber);
Console.WriteLine(isValid); // trueusing PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", "US");
var numberType = phoneNumberUtil.GetNumberType(phoneNumber);
Console.WriteLine(numberType); // PhoneNumberType.FIXED_LINE_OR_MOBILESee PhoneNumberType.cs for the various possible types of phone numbers
using PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", null);
var regionCode = phoneNumberUtil.GetRegionCodeForNumber(phoneNumber);
Console.WriteLine(regionCode); // USusing PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var geocoder = PhoneNumberOfflineGeocoder.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+12128120000", null);
var description = geocoder.GetDescriptionForNumber(phoneNumber, Locale.English);
Console.WriteLine(description); // New York, NYThe lookup is entirely offline. Detail varies by region — some yield a city, others only a state or the country name — and non-geographic or invalid numbers return the country name or an empty string. Pass a user region to omit it from the description for local numbers, or use GetDescriptionForValidNumber to skip the internal validity check.
using PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var timeZonesMapper = PhoneNumberToTimeZonesMapper.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+12128120000", null);
var timeZones = timeZonesMapper.GetTimeZonesForNumber(phoneNumber);
Console.WriteLine(string.Join(", ", timeZones)); // America/New_YorkReturns a List of IANA time zone identifiers. For numbers that span multiple time zones (e.g. a country-level lookup), the list will contain more than one entry. Returns ["Etc/Unknown"] for invalid or unrecognised numbers.
Use GetTimeZonesForGeographicalNumber instead if you have already validated the number and want to skip the internal type check.
using PhoneNumbers;
var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var carrierMapper = PhoneNumberToCarrierMapper.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+917503397672", null);
var carrierName = carrierMapper.GetNameForNumber(phoneNumber, Locale.English);
Console.WriteLine(carrierName); // AircelNote: Carrier data reflects the original network allocation. If the country supports mobile number portability, the number may have since moved to a different carrier. Use
GetSafeDisplayNameto return an empty string in those regions.
See PhoneNumberUtil.cs for the various methods and properties available.
A lot of the functionality depends on updated metadata that is published by the google repository, see example here.
This means that if you don't keep the package up to date, methods like IsValidNumber will return false for newer numbers that rely on the updated metadata
Therefore, we recommend you keep this nuget package as up to date as possible using automated means (such as dependabot) as metadata changes published by the google repository is frequent, usually a few times a month.
For more information on metadata usage, please refer to the main repository faq
# Every project, every target framework.
dotnet test csharp/PhoneNumbers.slnx
# Faster, and what the pull request check runs.
dotnet test csharp/PhoneNumbers.slnx -p:TargetFrameworks=net10.0The binary metadata the library reads at run time is generated during the build, so a plain
dotnet build is all that is needed first — there is no separate generation step.
See CONTRIBUTING.md for the build settings that will fail CI if missed.
The create_new_release_on_new_metadata_update workflow runs daily at 08:00 UTC and drives lib/github-actions-metadata-update.sh. When the latest google/libphonenumber release is newer than the published NuGet package, it copies the upstream resources/ (less upstream's metadata/ csv tables, which nothing here reads), regenerates resources/locale/country_names.txt, adds a CHANGELOG.md entry for the release, then commits, pushes to a metadata-update/* branch and opens a PR against main with auto-merge deliberately left off. The push and PR authenticate as the dedicated libphonenumber-csharp-bot account (via the BOT_ACCESS_TOKEN secret) rather than the default GITHUB_TOKEN, since GitHub requires a maintainer to manually approve workflow runs on PRs opened with GITHUB_TOKEN.
A maintainer reviews and merges that PR, which is how a metadata release ships. If nobody has merged it by the next daily run, that run enables auto-merge so the release still goes out — a sync is never left stalled because nobody was around. Once it merges, [finalize_metadata_release](https://github.com/twcclegg/libphonenumber-csharp/actions/workflows/finalize
No open issues yet, or sync has not completed.