KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Say I want to check whether there are at least N elements in a collection. Is this better than doing? Count() >= N Using: public static bool AtLeast<T>(this IEnumerable<T> enumerable, int max) { int count = 0; return enumerable.Any(item => ++count >= max); } Or even public static bool Equals<T>(this IEnumerable<T> enumerable, int amount) { return enumerable.Take(amount).Count() == amount; } How could I benchmark this? /// <summary> /// Returns whether the enumerable has at least the provided amount of elements. /// </summary> public static bool HasAtLeast<T>(this IEnumerable<T> enumerable, int amount) { return enumerable.Take(amount).Count() == amount; } /// <summary> /// Returns whether the enumerable has at most the provided amount of elements. /// </summary> public static bool HasAtMost<T>(this IEnumerable<T> enumerable, int amount) { return enumerable.Take(amount + 1).Count() <= amount; }
Tags (comma-separated)
Save Edits
Cancel