猿问

LastOrDefault() 和 Last() 方法是否迭代列表中的每个元素?

LastOrDefault() 和 Last() 方法是否迭代列表中的每个元素以找到最后一个元素?或者他们是否通过基于元素的索引进行搜索来返回值?


繁华开满天机
浏览 166回答 1
1回答

紫衣仙女

如果它是一个类型,IList那么不,它不会迭代每个元素,因为这会非常低效。这就是他们所做的:public static TSource Last<TSource>(this IEnumerable<TSource> source) {&nbsp; &nbsp; &nbsp; &nbsp; if (source == null) throw Error.ArgumentNull("source");&nbsp; &nbsp; &nbsp; &nbsp; IList<TSource> list = source as IList<TSource>;&nbsp; &nbsp; &nbsp; &nbsp; if (list != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count = list.Count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count > 0) return list[count - 1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (IEnumerator<TSource> e = source.GetEnumerator()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.MoveNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TSource result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = e.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (e.MoveNext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; throw Error.NoElements();&nbsp; &nbsp; }和LastOrDefault:public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (source == null) throw Error.ArgumentNull("source");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IList<TSource> list = source as IList<TSource>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count = list.Count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count > 0) return list[count - 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (IEnumerator<TSource> e = source.GetEnumerator()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.MoveNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TSource result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = e.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (e.MoveNext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return default(TSource);}请注意,由于每种方法都有几个重载,我在上面只展示了其中一个,但是如果您对其他方法感兴趣
随时随地看视频慕课网APP
我要回答