[Mono-dev] Trivial Enumerable Optimizations

Miguel de Icaza miguel at novell.com
Fri May 9 17:05:19 EDT 2008


> So is it possible to further optimize the LongCount() extension method?
> Perhaps something like this?
> 
>     public static long LongCount<TSource> (this IEnumerable<TSource> source)
>     {
>        // as before...
>        ICollection<TSource> c = source as ICollection<TSource>;
>        if (c != null) {
>            try {
>                return c.Count;
>            }
>            catch {
>                // ignore; collection has > int.MaxValue elements, 
>                // so count manually below.
>            }
>        }
>        // as before...
>     }

The problem is that there is no such rule that states that c.Count will
throw an exception if it has more than certain number of elements.

Miguel.



More information about the Mono-devel-list mailing list