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

To create a password with uppercase, lowercase letters, numbers and symbols in C#, you can use the following code:

main.cs
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        const string lowercase = "abcdefghijklmnopqrstuvwxyz";
        const string digits = "0123456789";
        const string symbols = "!@#$%^&*()_=+-[]{};:'\"\\|,.<>/?";

        const string allChars = uppercase + lowercase + digits + symbols;

        var random = new Random();
        var password = new string(Enumerable.Repeat(allChars, 12)
          .Select(s => s[random.Next(s.Length)]).ToArray());

        Console.WriteLine(password);
    }
}
600 chars
22 lines

In this example, we define four different strings containing uppercase letters, lowercase letters, digits, and symbols respectively. We then concatenate these strings into a single string called allChars.

Next, we create a Random object and use the Enumerable.Repeat method to create an array of 12 characters randomly chosen from allChars. Finally, we use the string constructor to create a new string from the array of characters, and print it to the console.

This will generate a random password with a length of 12 characters containing a mix of uppercase letters, lowercase letters, numbers, and symbols.

gistlibby LogSnag