Featured image of post Find() vs FirstOrDefault() in C#

Find() vs FirstOrDefault() in C#

Collection and EntityFramework performance

Ref: https://rules.sonarsource.com/csharp/RSPEC-6602/
Ref: https://learn.microsoft.com/en-us/ef/ef6/querying/

My favourite code analyser (SonarLint) told me something this week that I was not aware of:
List.Find() can be faster than IEnumerable.FirstOrDefault() for List objects.

For small collections, the performance difference may be minor, but for large collections, it can make a noticeable difference. The same applies for ImmutableList and arrays too. SonarLint claims to have measured at least 2x improvement in the execution time. Also for database queries it could make a difference as FirstOrDefault() will always execute the query against the database while Find() will try the memory db context first.

Update 25.09.2024: .NET9 appears to have optimized IEnumerable.FirstOrDefault() - making it faster than List.Find() (ref: https://www.youtube.com/watch?v=UfjRoshu23w)

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy