Exemplo n.º 1
0
        public int GetCount(bool onlyIfCheap)
        {
            IIListProvider <TElement> listProv = source as IIListProvider <TElement>;

            if (listProv != null)
            {
                return(listProv.GetCount(onlyIfCheap));
            }
            return(!onlyIfCheap || source is ICollection <TElement> || source is ICollection?source.Count() : -1);
        }
 public int GetCount(bool onlyIfCheap)
 {
     if (onlyIfCheap)
     {
         return(_source switch
         {
             IIListProvider <TSource> listProv => listProv.GetCount(onlyIfCheap: true),
             ICollection <TSource> colT => colT.Count,
             ICollection col => col.Count,
             _ => - 1,
         });
Exemplo n.º 3
0
            public override int GetCount(bool onlyIfCheap)
            {
                IIListProvider <TSource> listProv = _source as IIListProvider <TSource>;

                if (listProv != null)
                {
                    int count = listProv.GetCount(onlyIfCheap);
                    return(count == -1 ? -1 : count + (_appended == null ? 0 : _appended.Count) + (_prepended == null ? 0 : _prepended.Count));
                }

                return(!onlyIfCheap || _source is ICollection <TSource>?_source.Count() + (_appended == null ? 0 : _appended.Count) + (_prepended == null ? 0 : _prepended.Count) : -1);
            }
Exemplo n.º 4
0
            public int GetCount(bool onlyIfCheap)
            {
                int count;

                if (!onlyIfCheap || _source is ICollection <TSource> || _source is ICollection)
                {
                    count = _source.Count();
                }
                else
                {
                    IIListProvider <TSource> listProv = _source as IIListProvider <TSource>;
                    count = listProv == null ? -1 : listProv.GetCount(onlyIfCheap: true);
                }
                return(count == 0 ? 1 : count);
            }
Exemplo n.º 5
0
            public int GetCount(bool onlyIfCheap)
            {
                if (onlyIfCheap)
                {
                    IIListProvider <TSource> listProv = _source as IIListProvider <TSource>;
                    if (listProv != null)
                    {
                        return(listProv.GetCount(onlyIfCheap: true));
                    }
                    if (!(_source is ICollection <TSource>) && !(_source is ICollection))
                    {
                        return(-1);
                    }
                }

                return(_source.Count());
            }
Exemplo n.º 6
0
Arquivo: Count.cs Projeto: jnm2/corefx
        public static int Count <TSource>(this IEnumerable <TSource> source)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            ICollection <TSource> collectionoft = source as ICollection <TSource>;

            if (collectionoft != null)
            {
                return(collectionoft.Count);
            }

            IIListProvider <TSource> listProv = source as IIListProvider <TSource>;

            if (listProv != null)
            {
                return(listProv.GetCount(onlyIfCheap: false));
            }

            ICollection collection = source as ICollection;

            if (collection != null)
            {
                return(collection.Count);
            }

            int count = 0;

            using (IEnumerator <TSource> e = source.GetEnumerator())
            {
                checked
                {
                    while (e.MoveNext())
                    {
                        count++;
                    }
                }
            }

            return(count);
        }