.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); // 空值
以上是 .NET Framework LastOrDefault 的全部内容, 来源链接: utcz.com/z/340687.html