First(), Last(), Any()
Helper methods to simplify development. Prevent common LINQ performance mistakes.
Source
public static T First<T>(this IList<T> list) {
return list[0];
}
public static T Last<T>(this IList<T> list) {
return list[list.Count - 1];
}
public static bool Any<T>(this ICollection<T> list) {
return list.Count > 0;
}
Example
var l = new List<int> { 4, 12, 562, 1 };
Console.WriteLine(l.First()); // 4
Console.WriteLine(l.Last()); // 1
Console.WriteLine(l.Any()); // true
Author: Fons Sonnemans
Submitted on: 24 jan. 2014
Language: C#
Type: System.Collections.Generic.IList<T>
Views: 6061