Docs: Anonymous types equality and order of properties

Created on 21 May 2020  Â·  6Comments  Â·  Source: dotnet/docs

two anonymous objects with the same Properties in different order should be equal ?

The Equals and GetHashcode methods on anonymous types override the methods inherited from object, and are defined in terms of the Equals and GetHashcode of the properties, so that two instances of the same anonymous type are equal if and only if all their properties are equal.

var p1 = new { Name = "Lawnmower", Price = 495.00 };
var p2 = new { Name = "Lawnmower", Price = 495.00 };
var p3 = new { Price = 495.00, Name = "Lawnmower" };

Console.WriteLine($"are equal {p1.Equals(p2)}");// true
Console.WriteLine($"are equal {p1.Equals(p3)}"); //false

Within the same program, two anonymous object initializers that specify a sequence of properties of the same names and compile-time types in the same order will produce instances of the same anonymous type.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide Technology - C# / VB Specification P3 Pri2 csharp-spetech doc-enhancement dotnet-csharprod

Most helpful comment

@svick I agree that no additional clause is needed.

The example is informative text, and it may be worth adding an equality example as well as the assignment example.

All 6 comments

Hi @jahbenjah,

The order matters. In fact, with a different order - they are considered different types. With the example above, p3 is a different anonymous type from p1 and p2 - as they are initialized in a different order.

Within the same program, two anonymous object initializers that specify a sequence of properties of the same names and compile-time types in the same order will produce instances of the same anonymous type.

Hi @IEvangelist

sorry, i update my question. I think this phrase _"so that two instances of the same anonymous type are equal if and only if all their properties are equal"_ must include the order of properties.

Hi @IEvangelist

sorry, i update my question. I think this phrase _"so that two instances of the same anonymous type are equal if and only if all their properties are equal"_ must include the order of properties.

Ah, ok. I see what you're saying now. So the statement would need to be updated to include order.

The Equals and GetHashcode methods on anonymous types override the methods inherited from object, and are defined in terms of the Equals and GetHashcode of the properties, so that two instances of the same anonymous type are equal if and only if all their properties are equal and initialized in the same order.

We'll happily review a pull request, I've updated this issue and marked it as "up for grabs" - thank you.

I'm removing the up-for-grabs label (for now at least)

The ECMA standards committee is currently working on a plan to to update the spec and the standard. We'll be surfacing that soon.

I think the spec is correct as is. Since p1 and p3 are not instances of the same anonymous type, there is no reason to add the additional clause to the spec.

@svick I agree that no additional clause is needed.

The example is informative text, and it may be worth adding an equality example as well as the assignment example.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LJ9999 picture LJ9999  Â·  3Comments

ike86 picture ike86  Â·  3Comments

sebagomez picture sebagomez  Â·  3Comments

LJ9999 picture LJ9999  Â·  3Comments

tswett picture tswett  Â·  3Comments