Hi .NET Folks
Today we added some new performance-related rules for C# and VB.NET. Most of these are very simple changes that allow your code to run more efficiently, especially when you are working with large collections. We benchmarked all of them to check that they made a significant difference and we’ve included the details of those tests in the Resources section of the rule description. The impact of following these guidelines will depend on usage, and we hope that they can help you enjoy cleaner, faster and more energy-efficient software.
- “Find” method should be used instead of the “FirstOrDefault” extension
- The collection-specific “TrueForAll” method should be used instead of the “All” extension
- Collection-specific “Exists” method should be used instead of the “Any” extension
- The collection should be filtered before sorting by using “Where” before “OrderBy”
- Prefer indexing instead of “Enumerable” methods on types implementing “IList”
- “Min/Max” properties of “Set” types should be used instead of the “Enumerable” extension methods
- “StartsWith” and “EndsWith” overloads that take a “char” should be used instead of the ones that take a “string”
- The lambda parameter should be used instead of capturing arguments in “ConcurrentDictionary” methods
- “First” and “Last” properties of “LinkedList” should be used instead of the “First()” and “Last()” extension methods
- “Contains” should be used instead of “Any” for simple equality checks
- “string.Create” should be used instead of “FormattableString”
The new rules are available today on SonarCloud, the upcoming SonarQube 10.1 release and in the next release of SonarLint.
Please let us know what you think of these new rules and post a comment here if you have any questions.
Thanks
Tom