rspec/rules/S6602/why-dotnet.adoc

12 lines
1.1 KiB
Plaintext
Raw Normal View History

== 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]