Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move comparisons for equality of value types before equality comparisons of collections #1090

Open
RalfKoban opened this issue Oct 14, 2024 · 0 comments · May be fixed by #1104
Open

Move comparisons for equality of value types before equality comparisons of collections #1090

RalfKoban opened this issue Oct 14, 2024 · 0 comments · May be fixed by #1104

Comments

@RalfKoban
Copy link
Owner

RalfKoban commented Oct 14, 2024

We should report that equality comparisons within logical AND conditions should be done by comparing values first and collections last.

Example:

public class SomeData
{
    public int Id { get; set; }
    public List<Data> Data { get; set; }

    public bool Equals(SomeData other)
    {
        return ((Data == other.Data) || (Data != null && Data.SequenceEqual(otherData))) && Id == other.Id;
    }
}

should be reported and changed into:

public class SomeData
{
    public int Id { get; set; }
    public List<Data> Data { get; set; }

    public bool Equals(SomeData other)
    {
        return Id == other.Id && ((Data == other.Data) || (Data != null && Data.SequenceEqual(otherData)));
    }
}

The reason is that comparison on value types is normally faster than comparing lists.

Note: The term "Value types" here include enums, Guids and strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
1 participant