A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2013 below:

CA2013: Do not use ReferenceEquals with value types (code analysis) - .NET

CA2013: Do not use ReferenceEquals with value types

In this article Property Value Rule ID CA2013 Title Do not use ReferenceEquals with value types Category Reliability Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As warning Cause

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

Rule description

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

How to fix violations

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


    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 warnings

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

See also

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.

Additional resources

In this article

Was this page helpful?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4