create a password with a custom length, uppercase letters and lowercase letters in csharp

Here's a simple example in C# to generate a random password with a custom length, containing uppercase and lowercase letters:

main.cs
using System;
using System.Security.Cryptography;

public static class PasswordGenerator
{
    private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();

    public static string GeneratePassword(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        var bytes = new byte[length];
        _rng.GetBytes(bytes);

        return new string(
            bytes.Select(x => chars[x % chars.Length])
                 .ToArray());
    }
}
516 chars
20 lines

You can then call this method and pass in the desired length of the password:

main.cs
int passwordLength = 12;
string password = PasswordGenerator.GeneratePassword(passwordLength);
Console.WriteLine(password);
124 chars
4 lines

This will produce a random password with 12 characters that contains both uppercase and lowercase letters. Note that this is just a simple example; in practice you would want to add additional complexity and randomness to the password generation process to increase its security.

gistlibby LogSnag