Generate Random Phone Numbers in C#
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C#
What: Generate random phone numbers in C# using Random and Bogus library. Covers US format, international format, and validation for .NET testing.
Try it: Use our interactive Phone-numbers generator or integrate this code into your C# application.
Generate random phone numbers in C# using Random and Bogus library. Covers US format, international format, and validation for .NET testing. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C++ , Ruby and Go or use our interactive web generator.
C# Code Example
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class PhoneNumberGenerator
{
private static readonly Random random = new Random();
// Generate random US phone number (xxx) xxx-xxxx
public static string RandomUsPhoneNumber()
{
int areaCode = random.Next(200, 1000);
int exchange = random.Next(200, 1000);
int lineNumber = random.Next(1000, 10000);
return $"({areaCode:D3}) {exchange:D3}-{lineNumber:D4}";
}
// Generate random phone number with country code
public static string RandomInternationalPhone(string countryCode = "+1")
{
int areaCode = random.Next(200, 1000);
int exchange = random.Next(200, 1000);
int lineNumber = random.Next(1000, 10000);
return $"{countryCode} ({areaCode:D3}) {exchange:D3}-{lineNumber:D4}";
}
// Generate phone number without formatting
public static string RandomPhoneDigits(int length = 10)
{
var sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
sb.Append(random.Next(10));
}
return sb.ToString();
}
// Validate US phone number format
public static bool IsValidUsPhone(string phone)
{
return Regex.IsMatch(phone, @"^\(\d{3}\) \d{3}-\d{4}$");
}
// Generate multiple phone numbers
public static string[] GeneratePhoneNumbers(int count = 10)
{
return Enumerable.Range(0, count)
.Select(_ => RandomUsPhoneNumber())
.ToArray();
}
// Using Bogus library (Install-Package Bogus)
// using Bogus;
// var faker = new Faker();
// string phone = faker.Phone.PhoneNumber("(###) ###-####"); // (555) 123-4567
// string phoneE164 = faker.Phone.PhoneNumber("+1##########"); // +15551234567
public static void Main()
{
Console.WriteLine($"US Phone: {RandomUsPhoneNumber()}");
Console.WriteLine($"International: {RandomInternationalPhone(\"+44\")}");
Console.WriteLine($"Digits only: {RandomPhoneDigits(10)}");
Console.WriteLine($"Valid: {IsValidUsPhone(\"(555) 123-4567\")}");
Console.WriteLine($"Batch: {string.Join(\", \", GeneratePhoneNumbers(3))}");
}
}
[EXPLANATION]
C# uses Random.Next(200, 1000) for generating area codes (200-999), exchanges (200-999), and line numbers (1000-9999). String interpolation with :D3 format specifier ensures 3-digit zero-padding. Regex.IsMatch() validates patterns. StringBuilder efficiently builds digit-only strings. Bogus library offers Phone.PhoneNumber() with custom format strings using # for digits. LINQ's Enumerable.Range() enables functional-style batch generation.
Expected Output
US Phone: (555) 234-5678 International: +44 (555) 234-5678 Digits only: 5551234567 Valid: True Batch: (234) 567-8901, (345) 678-9012, (456) 789-0123
Common Use Cases
- Generate phone numbers for ASP.NET Core user registration testing
- Create contact data for Entity Framework model testing
- Populate test databases with phone number data
- Test phone validation in .NET web applications
- Generate mock data for SMS service integration
Important Notes
-
:D3format specifier pads integers with leading zeros to 3 digits -
Bogus supports fluent syntax:
faker.Phone.PhoneNumber() -
StringBuilderis more efficient than string concatenation in loops -
Regex.IsMatch()is faster thanRegex.Match()for validation - Bogus format string: # = any digit (0-9), no need to escape special chars
Try Our Interactive Generator
Don't want to write code? Use our free web-based Phone-numbers generator with instant results.
TRY PHONE-NUMBERS GENERATOR →Other Programming Languages
View Phone-numbers generation code examples in PHP
View Phone-numbers generation code examples in JavaScript
View Phone-numbers generation code examples in Python
View Phone-numbers generation code examples in Java
View Phone-numbers generation code examples in C++
View Phone-numbers generation code examples in Ruby
View Phone-numbers generation code examples in Go