Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceEquals always returns false for struct comparison #41839

Open
marchewek opened this issue Jul 19, 2024 · 3 comments · May be fixed by #44624
Open

ReferenceEquals always returns false for struct comparison #41839

marchewek opened this issue Jul 19, 2024 · 3 comments · May be fixed by #44624
Labels
dotnet-csharp/svc fundamentals/subsvc help wanted Good for community contributors to help [up-for-grabs] in-pr This issue will be closed (fixed) by an active pull request.

Comments

@marchewek
Copy link

Type of issue

Other (describe below)

Description

The article attributes the fact that ReferenceEquals=false to the fact that a structure was copied, however it's not true - it will always return false, even in both examples (in which clearly no copy is involved); so it shouldn't be attributed to the fact of copying, but rather due to boxing ;)

public struct A
{
}

public class Assertions
{
	internal static A A1 = new A();

	public void Y()
	{
		Console.WriteLine(object.ReferenceEquals(A1, A1));
		Lol(ref A1);
	}

	private void Lol(ref A a)
	{
		Console.WriteLine(object.ReferenceEquals(A1, a));
	}
}

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/how-to-test-for-reference-equality-identity

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/programming-guide/statements-expressions-operators/how-to-test-for-reference-equality-identity.md

Document Version Independent Id

b8e7e74e-d529-2397-a121-2cb7618a6cac

Article author

@BillWagner

Metadata

  • ID: abfb3769-16f2-70c8-8b66-5fa0a11354c0
  • Service: dotnet-csharp
  • Sub-service: fundamentals
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Jul 19, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Jul 23, 2024
@BillWagner BillWagner added help wanted Good for community contributors to help [up-for-grabs] ⌚ Not Triaged Not triaged labels Jul 23, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Jul 23, 2024
@BillWagner
Copy link
Member

Hi @marchewek

I'll put this in the backlog so that we clarify the language. You're correct, but so is the current text. A boxing conversion does copy the struct (§10.2.9).

Boxing a value of a non-nullable-value-type consists of allocating an object instance and copying the value into that instance.

Yes, boxing conversions are involved. Yes, the struct is copied.

@marchewek
Copy link
Author

marchewek commented Jul 24, 2024

Yes, I know that structures are copied; it's just that ReferenceEquals returns false for a completely different reason; it would also return false for a single structure used as its parameters twice:

var x = new MyStruct();
object.ReferenceEquals(x, x); // returns false as well

so beginners might be misled by the current statement: the "the structures are copied therefore ReferenceEquals returns false" implies that "well, so if they weren't copied, it would return true, right?"; nope, it wouldn't, because the parameters are boxed and two different objects are being reference-equal-ed.

If the example's aim is to prove that these are separate copies, then maybe after the copying, the first variable could be changed and the example could show that the second variable remains unchanged.

@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Jan 30, 2025
@shethaadit
Copy link
Contributor

Hi @BillWagner, I have raised PR to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc help wanted Good for community contributors to help [up-for-grabs] in-pr This issue will be closed (fixed) by an active pull request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants