Generate Random Strings in C#
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C#
What: Generate random strings in C# using <code>RandomNumberGenerator</code> for cryptographically secure random data. Perfect for ASP.NET and .NET applications.
Try it: Use our interactive Strings generator or integrate this code into your C# application.
Generate random strings in C# using RandomNumberGenerator for cryptographically secure random data. Perfect for ASP.NET and .NET applications.
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.Security.Cryptography;
using System.Text;
class RandomStringGenerator
{
private const string Alphanumeric =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private const string HexChars = "0123456789abcdef";
public static string GenerateRandomString(int length, string customChars = null)
{
string chars = customChars ?? Alphanumeric;
using (var rng = RandomNumberGenerator.Create())
{
var result = new StringBuilder(length);
byte[] randomBytes = new byte[length * 4];
rng.GetBytes(randomBytes);
for (int i = 0; i < length; i++)
{
uint randomValue = BitConverter.ToUInt32(randomBytes, i * 4);
result.Append(chars[(int)(randomValue % chars.Length)]);
}
return result.ToString();
}
}
static void Main()
{
// Generate a 16-character alphanumeric string
string randomString = GenerateRandomString(16);
Console.WriteLine(randomString); // Example: aB3xY7mN2pQ9zR5t
// Generate a 12-character hex string
string hexString = GenerateRandomString(12, HexChars);
Console.WriteLine(hexString); // Example: a3f7b2e9c4d1
// Generate a 10-character lowercase string
string lowercaseString = GenerateRandomString(10, "abcdefghijklmnopqrstuvwxyz");
Console.WriteLine(lowercaseString); // Example: xmkpqrstuv
}
}
[EXPLANATION]
RandomNumberGenerator provides cryptographically secure random bytes. The method converts random bytes to indices for character selection, ensuring uniform distribution. StringBuilder optimizes string construction performance.
Expected Output
aB3xY7mN2pQ9zR5t a3f7b2e9c4d1 xmkpqrstuv
Common Use Cases
- ASP.NET Core session token generation
- Entity Framework unique identifiers
- Azure Functions correlation IDs
- WPF/WinForms temporary file naming
- Xamarin mobile app unique keys
- API authentication tokens
Important Notes
-
Use
RandomNumberGeneratorfor cryptographic security - Available in .NET Framework, .NET Core, and .NET 5+
-
Consider using
Guid.NewGuid()for UUID-like strings -
StringBuilderimproves string building performance
Try Our Interactive Generator
Don't want to write code? Use our free web-based Strings generator with instant results.
TRY STRINGS GENERATOR →Other Programming Languages
View Strings generation code examples in PHP
View Strings generation code examples in JavaScript
View Strings generation code examples in Python
View Strings generation code examples in Java
View Strings generation code examples in C++
View Strings generation code examples in Ruby
View Strings generation code examples in Go