C# Program – Generate Random Alphanumeric String

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.

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

Leave Your Comment

Liked this content?. Please spread the word :)