実際、それらはまったく同じではありません。最初は次のようになります。
public static TSource Last<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw Error.ArgumentNull("source");
IList<TSource> list = source as IList<TSource>;
if (list != null) {
int count = list.Count;
if (count > 0) return list[count - 1];
} else {
using (IEnumerator<TSource> enumerator = source.GetEnumerator()) {
if (enumerator.MoveNext()) {
TSource current;
do { current = enumerator.Current;}
while (enumerator.MoveNext());
return current;
}
}
}
throw Error.NoElements();
}
そしてもう一つは次のようなものです。
public static TSource Last<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
TSource last = default(TSource);
bool foundOne = false;
foreach (TSource value in source) {
if (predicate(value)) {
last = value;
foundOne = true;
}
}
if (!foundOne) throw Error.NoMatch();
return last;
}
PS .: 現在、著作権を侵害していないことを願っています。:S