Skip to content

Instantly share code, notes, and snippets.

@ViIvanov
Last active January 4, 2016 14:58
Show Gist options
  • Save ViIvanov/8637249 to your computer and use it in GitHub Desktop.
Save ViIvanov/8637249 to your computer and use it in GitHub Desktop.
Count(), Any()
void M<T>(ICollection<T> source) {
var a = source.Any(); // OK, statically known, that source is ICollection<T> and call is optimized
var b = source.Count(); // Warning, source.Count is better
}
bool M1<T>(IEnumerable<T> source) {
return source.Any(); // OK - one iteration in a worst case
}
int M1<T>(IEnumerable<T> source) {
return source.Count(); // OK - see above
}
void M2<T>(IEnumerable<T> source) {
if(source.Any()) { // // Warning - two iterations in a worst case
foreach(var item in source) {…
}//if
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment