meziantou / Meziantou.Analyzer

A Roslyn analyzer to enforce some good practices in C#.
MIT License
951 stars 49 forks source link

Reconsider MA0020 #751

Closed trejjam closed 1 month ago

trejjam commented 1 month ago

Version of the Meziantou.Analyzer NuGet package

2.0.163

Rule Identifier

MA0020

Target Framework

net9.0

C# Language version

13

Description

Hi, I saw Nick's video about using List<>.Find vs Linq.FirstOrDefault, and I think that it can be good to reconsider this rule.

TLDW: List<>.Find is faster than Linq.FirstOrDefault but only in <=net8.0, with net9.0 FirstOrDefault is optimized to use spans, and it beats List<>.Find.

It may seem reasonable to report it for <=net8.0, but it will create more work in the future. My thought is to remove this recommendation (for all versions) and reverse it (since net9)

Reproduction Steps

https://www.youtube.com/watch?v=UfjRoshu23w&t=623s

Other information

No response

trejjam commented 1 month ago

Reporting code: https://github.com/meziantou/Meziantou.Analyzer/blob/423b37fe510ca21bbfec2a647317c8c1828bac78/src/Meziantou.Analyzer/Rules/OptimizeLinqUsageAnalyzer.cs#L278

meziantou commented 1 month ago

Fixed by 40008ca9441275cf8a3d5b790215ffc5046f6eb7