linq-ElementAt


源码:

       public static TSource ElementAt(this IEnumerable source, int index) {
            if (source == null) throw Error.ArgumentNull("source");
            IList list = source as IList;
            if (list != null) return list[index];
            if (index < 0) throw Error.ArgumentOutOfRange("index");
            using (IEnumerator e = source.GetEnumerator()) {
                while (true) {
                    if (!e.MoveNext()) throw Error.ArgumentOutOfRange("index");
                    if (index == 0) return e.Current;
                    index--;
                }
            }
        }
 
        public static TSource ElementAtOrDefault(this IEnumerable source, int index) {
            if (source == null) throw Error.ArgumentNull("source");
            if (index >= 0) {
                IList list = source as IList;
                if (list != null) {
                    if (index < list.Count) return list[index];
                }
                else {
                    using (IEnumerator e = source.GetEnumerator()) {
                        while (true) {
                            if (!e.MoveNext()) break;
                            if (index == 0) return e.Current;
                            index--;
                        }
                    }
                }
            }
            return default(TSource);
        }

具体实现转换成了List,使用下标获取值,还有使用了using。