All rules
CA2013Reliability Enabled by default: As warning

Do not use ReferenceEquals with value types

Do not use ReferenceEquals with value types

Microsoft docs

Description

When comparing values using System.Object.ReferenceEquals, if objA and objB are value types, they are boxed before they are passed to the System.Object.ReferenceEquals method. This means that even if both objA and objB represent the same instance of a value type, the System.Object.ReferenceEquals method nevertheless returns false, as the following example shows.

Cause

Using System.Object.ReferenceEquals method to test one or more value types for equality.

How to fix violations

To fix the violation, replace it with a more appropriate equality check such as ==.

Example

    int int1 = 1, int2 = 1;

    // Violation occurs, returns false.
    Console.WriteLine(Object.ReferenceEquals(int1, int2));  // false

    // Use appropriate equality operator or method instead
    Console.WriteLine(int1 == int2);                        // true
    Console.WriteLine(object.Equals(int1, int2));           // true

When to suppress

It is not safe to suppress a warning from this rule. We recommend using the more appropriate equality operator, such as ==.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0