csharpstandard icon indicating copy to clipboard operation
csharpstandard copied to clipboard

Anonymous types equality and order of properties

Open jahbenjah opened this issue 5 years ago • 12 comments

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.

jahbenjah avatar May 21 '20 14:05 jahbenjah

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.

IEvangelist avatar May 21 '20 14:05 IEvangelist

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.

jahbenjah avatar May 21 '20 14:05 jahbenjah

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.

IEvangelist avatar May 21 '20 14:05 IEvangelist

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.

BillWagner avatar May 26 '20 00:05 BillWagner

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 avatar May 26 '20 00:05 svick

@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.

BillWagner avatar May 26 '20 00:05 BillWagner

This issue has been closed as part of the issue backlog grooming process outlined in dotnet/docs#22351.

That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.

dotnet-bot avatar Jan 25 '21 15:01 dotnet-bot

@BillWagner Do you still think it might be worth updating the example in the spec? If you do, you should probably reopen this issue.

svick avatar Jan 25 '21 21:01 svick

reopening to reassign to the committee for review.

BillWagner avatar Jan 25 '21 21:01 BillWagner

@jskeet

This may be below the bar. The "in the same order" is already in informative text for assignment, but not for equality.

BillWagner avatar Apr 21 '21 15:04 BillWagner

I think we probably do want to improve this, but I'm happy to close it as a duplicate of #114.

Labeling as "meeting:discuss" and "meeting:proposal" to ratify that decision.

jskeet avatar Apr 21 '21 20:04 jskeet

Revisiting this many moons later - I still want to close this as a duplicate of #114. I'm re-adding "meeting: discuss" to make it easier to find in our meeting.

jskeet avatar Sep 12 '24 09:09 jskeet

Closing as a duplicate.

jskeet avatar Oct 02 '24 20:10 jskeet