rspec/rules/S6602/why-dotnet.adoc
Martin Strecker 823fcb14f6
Modify rule S6602: Update benchmark (#4438)
* Update benchmark

* Update why-dotnet.adoc

* Remove from SonarWay

* Apply suggestions from code review

Co-authored-by: Cristian <67206480+CristianAmbrosini@users.noreply.github.com>

---------

Co-authored-by: Cristian <67206480+CristianAmbrosini@users.noreply.github.com>
2024-10-29 12:33:52 +01:00

12 lines
1.1 KiB
Plaintext

== Why is this an issue?
Both the `List.Find` method and the `Enumerable.FirstOrDefault` method can be used to locate the first element that meets a specified condition within a collection. However, for `List` objects, `List.Find` may offer superior performance compared to `Enumerable.FirstOrDefault`. While the performance difference might be negligible for small collections, it can become significant for larger collections. This observation also holds true for `ImmutableList` and arrays.
It is important to enable this rule with caution, as performance outcomes can vary significantly across different runtimes. Notably, the https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-9/#collections[performance improvements in .NET 9] have brought `FirstOrDefault` closer to the performance of collection-specific `Find` methods in most scenarios.
*Applies to*
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find[List]
* https://learn.microsoft.com/en-us/dotnet/api/system.array.find[Array]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.immutable.immutablelist-1.find[ImmutableList]