Skip to content

Instantly share code, notes, and snippets.

Created June 16, 2011 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1028862 to your computer and use it in GitHub Desktop.
Save anonymous/1028862 to your computer and use it in GitHub Desktop.
public static int Count<TSource>(this IEnumerable<TSource> source) {
if (source == null) {
throw Error.ArgumentNull("source");
}
var collectionT = source as ICollection<TSource>;
if (collectionT != null) {
return collectionT.Count;
}
var collection = source as ICollection;
if (collection != null) {
return collection.Count;
}
int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator()) {
while (enumerator.MoveNext()) {
num++;
}
}
return num;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment