create a password with lowercase letters in csharp

Here's one way to generate a secure password with lowercase letters in C#:

main.cs
public static string GeneratePassword(int length)
{
    const string chars = "abcdefghijklmnopqrstuvwxyz";
    var random = new Random();
    var password = new string(Enumerable.Repeat(chars, length)
        .Select(s => s[random.Next(s.Length)]).ToArray());
    return password;
}
283 chars
9 lines

This method uses the System.Random class to select random characters from a string of lowercase letters ("abcdefghijklmnopqrstuvwxyz") to build up the password. The length of the password is passed in as a parameter.

To use this method, you could call it from your code like so:

main.cs
var password = GeneratePassword(12);
Console.WriteLine(password);
66 chars
3 lines

This would generate and print out a random 12-character password made up of lowercase letters. You could adjust the length parameter to generate passwords of different lengths, and you could modify the chars string to include additional character sets (e.g., uppercase letters, digits, symbols) for added complexity.

gistlibby LogSnag