Subscribe to our YouTube Channel for more Videos

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);

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.