Skip to content

ReferenceEquals always returns false for struct comparison #41839

Closed
@marchewek

Description

@marchewek

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotnet-csharp/svcfundamentals/subsvchelp wantedGood for community contributors to help [up-for-grabs]in-prThis issue will be closed (fixed) by an active pull request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions