예제 #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,
         });
예제 #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);
            }
예제 #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);
            }
예제 #5
0
파일: Reverse.cs 프로젝트: rogeryu23/corefx
            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());
            }
예제 #6
0
파일: Count.cs 프로젝트: 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);
        }