JetBrains Rider 2026.1 Help

Code inspection: Replace with LastOrDefault($args$)

This inspection reports a conditional expression that checks Any(...) before calling Last(...), and returns default(T) when there is no match. That pattern is equivalent to LastOrDefault(...).

Example

var result = items.Any(x => x.IsValid) ? items.Last(x => x.IsValid) : default(MyType);
var result = items.LastOrDefault(x => x.IsValid);

Quick-fix

Replace the Any(...) ? Last(...) : default(T) pattern with LastOrDefault(...).

29 March 2026