dotnet / csharpstandard

Working space for ECMA-TC49-TG2, the C# standard committee.
Creative Commons Attribution 4.0 International
706 stars 84 forks source link

Anonymous types equality and order of properties #266

Open jahbenjah opened 4 years ago

jahbenjah commented 4 years ago

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.

IEvangelist commented 4 years ago

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.

jahbenjah commented 4 years ago

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.

IEvangelist commented 4 years ago

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.

BillWagner commented 4 years ago

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.

svick commented 4 years ago

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.

BillWagner commented 4 years ago

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

dotnet-bot commented 3 years ago

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.

svick commented 3 years ago

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

BillWagner commented 3 years ago

reopening to reassign to the committee for review.

BillWagner commented 3 years ago

@jskeet

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

jskeet commented 3 years ago

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 commented 5 days ago

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.