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.

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

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: