示例#1
0
            public TResult TryGetElementAt(int index, out bool found)
            {
                bool    sourceFound;
                TSource input = _source.TryGetElementAt(index, out sourceFound);

                found = sourceFound;
                return(sourceFound ? _selector(input) : default(TResult));
            }
示例#2
0
        public static TSource ElementAt <TSource>(this IEnumerable <TSource> source, int index)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            IPartition <TSource> partition = source as IPartition <TSource>;

            if (partition != null)
            {
                bool    found;
                TSource element = partition.TryGetElementAt(index, out found);
                if (found)
                {
                    return(element);
                }
            }
            else
            {
                IList <TSource> list = source as IList <TSource>;
                if (list != null)
                {
                    return(list[index]);
                }

                if (index >= 0)
                {
                    using (IEnumerator <TSource> e = source.GetEnumerator())
                    {
                        while (e.MoveNext())
                        {
                            if (index == 0)
                            {
                                return(e.Current);
                            }

                            index--;
                        }
                    }
                }
            }

            throw Error.ArgumentOutOfRange(nameof(index));
        }
示例#3
0
        public static TSource ElementAtOrDefault <TSource>(this IEnumerable <TSource> source, int index)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            IPartition <TSource> partition = source as IPartition <TSource>;

            if (partition != null)
            {
                bool found;
                return(partition.TryGetElementAt(index, out found));
            }
            if (index >= 0)
            {
                IList <TSource> list = source as IList <TSource>;
                if (list != null)
                {
                    if (index < list.Count)
                    {
                        return(list[index]);
                    }
                }
                else
                {
                    using (IEnumerator <TSource> e = source.GetEnumerator())
                    {
                        while (e.MoveNext())
                        {
                            if (index == 0)
                            {
                                return(e.Current);
                            }
                            index--;
                        }
                    }
                }
            }
            return(default(TSource));
        }