本文向大家介绍.NET Framework LastOrDefault,包括了.NET Framework LastOrDefault的使用技巧和注意事项,需要的朋友参考一下
示例
var numbers = new[] {1,2,3,4,5};
var lastNumber = numbers.LastOrDefault();
Console.WriteLine(lastNumber); //5
var lastEvenNumber = numbers.LastOrDefault(n => (n & 1) == 0);
Console.WriteLine(lastEvenNumber); //4
var lastNegativeNumber = numbers.LastOrDefault(n => n < 0);
Console.WriteLine(lastNegativeNumber); //0
var words = new[] { "one", "two", "three", "four", "five" };
var lastWord = words.LastOrDefault();
Console.WriteLine(lastWord); // 五
var lastLongWord = words.LastOrDefault(w =>w.Length> 4);
Console.WriteLine(lastLongWord); // 三
var lastMissingWord = words.LastOrDefault(w =>w.Length> 5);
Console.WriteLine(lastMissingWord); // 空值