create a password with uppercase, lowercase letters and numbers in csharp

Here is a code snippet to generate a password with uppercase, lowercase letters and numbers in C#:

main.cs
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        int length = 8; // set the length of the password
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // define the characters to be used in the password
        var random = new Random();
        var password = new string(Enumerable.Repeat(chars, length)
          .Select(s => s[random.Next(s.Length)]).ToArray()); // generate the random password
        
        Console.WriteLine(password);
    }
}
546 chars
17 lines

This code creates a password with 8 characters in total, made up of uppercase letters, lowercase letters, and numbers. You can adjust the length variable to make the password longer or shorter. The password is generated using a randomly selected character from the defined character set using the Random class in C#.

gistlibby LogSnag