private static Random _random = new Random(); public static T GetRandom<T>(this List<T> list) { if (list.Count == 0) return default(T); if (list.Count == 1) return list[0]; return list[_random.Next(0, list.Count)]; }
Very useful in a broad range of scenarios when you want to get a random element from a list.
Just call myList.GetRandom()
Back to extensions list
No comments:
Post a Comment