Fastest way to get a random value from a string array in C#?
问题 What's the fastest way to get a random value from a string array in C# on the .net 2.0 framework? I figured they might have had this: string[] fileLines = File.ReadAllLines(filePath); fileLines.GetRandomValue(); Yes, I know GetRandomValue() is not an actual method, is there something similar that's more or less equally short and sweet? 回答1: Not built in, but easy enough to add... static readonly Random rand = new Random(); public static T GetRandomValue<T>(T[] values) { lock(rand) { return