dotnet / roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.

Home Page:https://josefpihrt.github.io/docs/roslynator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat(List): use TrueForAll instead of All

dylanvdmerwe opened this issue · comments

Product and Version Used:
4.12.0

Both the List.TrueForAll method and the IEnumerable.All method can be used to check if all list elements satisfy a given condition in a collection. However, List.TrueForAll can be faster than IEnumerable.All for List objects. The performance difference may be minor for small collections, but for large collections, it can be noticeable. Memory allocations are also much less with TrueForAll.

Original

public bool AreAllEven(List<int> data) =>
    data.All(x => x % 2 == 0);

public bool AreAllEven(int[] data) =>
    data.All(x => x % 2 == 0);

Fix

public bool AreAllEven(List<int> data) =>
    data.TrueForAll(x => x % 2 == 0);

public bool AreAllEven(int[] data) =>
    Array.TrueForAll(data, x => x % 2 == 0);

Note should also work for classes that inherit from List.