You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Maybe this rule for code targeting .NET 8.0 or below, and a new rule "FirstOrDefault" method should be used instead of the "Find" extension for code targeting .NET 9.0 or above?
marco-carvalho
changed the title
Fix S6602: "Use 'Find' instead of 'FirstOrDefault'" shouldn't be used in .NET 9.0
Fix S6602: "'Find' method should be used instead of the 'FirstOrDefault' extension" shouldn't be used in .NET 9.0
Sep 17, 2024
List.Find has been unchanged for a long time and reads the list's fields on every iteration of the loop, while FirstOrDefault was recently changed to read the fields once and efficiently loop over the resulting span. Perhaps dotnet/runtime should optimize List.Find too.
Note that FirstOrDefault's optimization may cause "impossible" values to be passed to the callback if the callback removes items from the list (which is wrong, but happened to work).
A rule to change Find back to FirstOrDefault would be bad churn. This kind of micro-optimizations tends to be unstable across versions.
Thank you for pointing this out.
We will take a look when we prepare for the .NET 9 release!
Have a great day!
sebastien-marichal
changed the title
Fix S6602: "'Find' method should be used instead of the 'FirstOrDefault' extension" shouldn't be used in .NET 9.0
Fix S6602 FP: FirstOrDefault is faster than Find in .NET 9
Sep 18, 2024
Hello,
The rule S6602, which suggests using
Find
instead ofFirstOrDefault
, may no longer be applicable for projects targeting .NET 9.0.Recent benchmarks indicate that starting with .NET 9.0,
FirstOrDefault
is actually faster thanFind
. Below are the benchmark results.Code:
Results:
The text was updated successfully, but these errors were encountered: