C# Tips and Tricks #11 – Generate random alphanumeric string

There are times when the developers would encounter scenarios where they need to generate a random alphanumeric string to be used with in their application.

How to generate random alphanumeric string in C# ?

Here’s a sample code snippet demonstrating how to do it using LINQ Query in C#.

using System;
using System.Linq;
namespace ConsoleApp1
{
    class Program
    {
        private static Random random = new Random();
        static void Main(string[] args)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var result = (from m in Enumerable.Repeat(chars, 10)
                          select m[random.Next(m.Length)]).ToArray();
            string resultStr = new string(result);
            Console.WriteLine(resultStr);

            Console.ReadLine();
        }
   
}
}
   
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.