示例#1
0
        public static TSource First <TSource>(this IEnumerable <TSource> source)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            IPartition <TSource> partition = source as IPartition <TSource>;

            if (partition != null)
            {
                return(partition.First());
            }
            IList <TSource> list = source as IList <TSource>;

            if (list != null)
            {
                if (list.Count > 0)
                {
                    return(list[0]);
                }
            }
            else
            {
                using (IEnumerator <TSource> e = source.GetEnumerator())
                {
                    if (e.MoveNext())
                    {
                        return(e.Current);
                    }
                }
            }
            throw Error.NoElements();
        }