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.
We should report that equality comparisons within logical AND conditions should be done by comparing values first and collections last.
Example:
should be reported and changed into:
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.