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

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

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...