更新时间:2023-12-05 11:23:46
我听说LINQ是新的黑色,所以这里的使用LINQ我尝试:
I heard LINQ is the new black, so here's my attempt using LINQ:
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
(注:的使用Random类的使这个不适合任何与安全有关的,如创建密码或令牌结果
           使用RNGCryptoServiceProvider类,如果你需要一个强大的随机数生成器)
(Note: The use of the Random class makes this unsuitable for anything security related, such as creating passwords or tokens.
Use the RNGCryptoServiceProvider class if you need a strong random number generator.)