Skip to content

Commit f54eec3

Browse files
shethaaditAdit ShethBillWagner
authored
Clarify ReferenceEquals Behavior for Value Types in Docs (#44624)
* Fixed bug 41839. * Apply suggestions from code review --------- Co-authored-by: Adit Sheth <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent e78ef00 commit f54eec3

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

docs/csharp/programming-guide/statements-expressions-operators/how-to-test-for-reference-equality-identity.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ You do not have to implement any custom logic to support reference equality comp
1414

1515
The following example shows how to determine whether two variables have *reference equality*, which means that they refer to the same object in memory.
1616

17-
The example also shows why <xref:System.Object.ReferenceEquals%2A?displayProperty=nameWithType> always returns `false` for value types and why you should not use <xref:System.Object.ReferenceEquals%2A> to determine string equality.
17+
The example also shows why <xref:System.Object.ReferenceEquals%2A?displayProperty=nameWithType> always returns `false` for value types. This is due to **boxing**, which creates separate object instances for each value type argument. Additionally, you should not use <xref:System.Object.ReferenceEquals%2A> to determine string equality.
1818

1919
## Example
2020

2121
[!code-csharp[TestingReferenceEquality](snippets/how-to-test-for-reference-equality-identity/Program.cs)]
2222

2323
The implementation of `Equals` in the <xref:System.Object?displayProperty=nameWithType> universal base class also performs a reference equality check, but it is best not to use this because, if a class happens to override the method, the results might not be what you expect. The same is true for the `==` and `!=` operators. When they are operating on reference types, the default behavior of `==` and `!=` is to perform a reference equality check. However, derived classes can overload the operator to perform a value equality check. To minimize the potential for error, it is best to always use <xref:System.Object.ReferenceEquals%2A> when you have to determine whether two objects have reference equality.
2424

25-
Constant strings within the same assembly are always interned by the runtime. That is, only one instance of each unique literal string is maintained. However, the runtime does not guarantee that strings created at run time are interned, nor does it guarantee that two equal constant strings in different assemblies are interned.
25+
Constant strings within the same assembly are always interned by the runtime. That is, only one instance of each unique literal string is maintained. However, the runtime does not guarantee that strings created at run time are interned, nor does it guarantee that two equal constant strings in different assemblies are interned.
26+
27+
> [!NOTE]
28+
> `ReferenceEquals` returns `false` for value types due to **boxing**, as each argument is independently boxed into a separate object.
2629
2730
## See also
2831

docs/csharp/programming-guide/statements-expressions-operators/snippets/how-to-test-for-reference-equality-identity/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static void Main()
5252

5353
TestStruct tsC = new TestStruct( 1, "TestStruct 1");
5454

55-
// Value types are copied on assignment. tsD and tsC have
56-
// the same values but are not the same object.
55+
// Value types are boxed into separate objects when passed to ReferenceEquals.
56+
// Even if the same variable is used twice, boxing ensures they are different instances.
5757
TestStruct tsD = tsC;
5858
Console.WriteLine("After assignment: ReferenceEquals(tsC, tsD) = {0}",
5959
Object.ReferenceEquals(tsC, tsD)); // false

0 commit comments

Comments
 (0)