C# Program – Generate Random Alphanumeric String


This content was proof-read by Grammarly. You can Try Grammary for FREE

This post explains how you can generate a random alphanumeric string in C#.

Assume that you have a requirement where you have to generate a random alphanumeric string of 10 characters in C#. There are various reasons why you may want to develop an arbitrary string.


Are you a .NET Developer planning to integrate GraphQL for .NET in your Application? Check out this book BUY & DOWNLOAD GRAPHQL.NET COOKBOOK

How to Generate Random Alphanumeric String in C#?

Here’s a sample code snippet demonstrating how you can generate a random alphanumeric string in C#.

using System;
using System.Linq;
using static System.Linq.Enumerable;

namespace AbundantcodeSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            
            var randomString = Repeat(chars, 10)
                .Select(s => s[random.Next(s.Length)]).ToArray();
            
            Console.WriteLine(randomString);
        }
        
    }
}

Output

%d bloggers like this: