Metadata

ID: csharp-best-practices/reference-equals-value-types

Language: C#

Severity: Warning

Category: Best Practices

Description

The Object.ReferenceEquals method in C# is used to determine if two object references refer to the same object, not if the objects themselves are equal. This rule is important because value types in C# are not reference types. They are stored on the stack, not the heap, and each instance has its own copy of the data. Therefore, using Object.ReferenceEquals with value types will always return false because they are boxed into separate objects on the heap.

How to remediate

To avoid breaking this rule, you should not use Object.ReferenceEquals to compare value types. Instead, you can use the == operator or Object.Equals method, which are designed to compare the values of two objects, not their references. This is the correct way to compare value types in C#, and it will give you the expected behavior.

For example, instead of writing Object.ReferenceEquals(int1, int2), you should write int1 == int2 or Object.Equals(int1, int2). These expressions will correctly compare the values of int1 and int2, not their references.

Non-Compliant Code Examples

int int1 = 1
int int2 = 1;

Console.WriteLine(Object.ReferenceEquals(int1, int2));
int int1 = 1, int2 = 1;

Console.WriteLine(Object.ReferenceEquals(int1, int2));

Compliant Code Examples

int int1 = 1, int2 = 1;

Console.WriteLine(int1 == int2);
Console.WriteLine(object.Equals(int1, int2));
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security