How can I generate random alphanumeric strings?

后端 未结 30 2770
予麋鹿
予麋鹿 2020-11-22 03:17

How can I generate a random 8 character alphanumeric string in C#?

30条回答
  •  抹茶落季
    2020-11-22 04:04

    Another option could be to use Linq and aggregate random chars into a stringbuilder.

    var chars = "abcdefghijklmnopqrstuvwxyz123456789".ToArray();
    string pw = Enumerable.Range(0, passwordLength)
                          .Aggregate(
                              new StringBuilder(),
                              (sb, n) => sb.Append((chars[random.Next(chars.Length)])),
                              sb => sb.ToString());
    

提交回复
热议问题