Repository navigation
[Bug]: Mutability of record GetHashCode #9595
Description
Activity
I had to add Page Urls and Content source Urls, as it wouldn't allow create issue with out it. Even though Issue creating form says that they would be autofilled.
Record hasn't been specificized at all. The specification is still a draft at dotnet/csharpstandard#983 .
The linked document you are referencing is under the "rejected" folder. It isn't the version that serves for implementation. The accepted version is at https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/records.md .Record hasn't been specificized at all. The specification is still a draft at dotnet/csharpstandard#983 . The linked document you are referencing is under the "rejected" folder. It isn't the version that serves for implementation. The accepted version is at https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/records.md .
Thank you, updated links, though question still remains.
This is indeed confusing, but the spec/proposal allows it:
The synthesized override of
GetHashCode()returns anintresult of combining the following values:- For each instance field
fieldNin the record type that is not inherited, the value of
System.Collections.Generic.EqualityComparer<TN>.Default.GetHashCode(fieldN)whereTNis the field type, and - If there is a base record type, the value of
base.GetHashCode(); otherwise
the value ofSystem.Collections.Generic.EqualityComparer<System.Type>.Default.GetHashCode(EqualityContract).
Since properties are backed by fields, point 1 applies.
IMHO, there should be a compiler-generated warning saying you should implement
GetHashCode()manually.- For each instance field
Specification states that record GetHashCode uses only positional properties (i.e. the ones specified in default constructor).
But in current implementation it uses at least public properties as well. Here is the failing testNo. Records use all backing fields in the type for equals/gethashcode. This is the design we wanted (as these represent value types, and all fields in a record are part of its value), and what hte impl does.
Type of issue
Spec incorrect
Description
Specification states that record GetHashCode uses only positional properties (i.e. the ones specified in default constructor).
But in current implementation it uses at least public properties as well. Here is the failing test
Page URL
https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/records.md#equality-members
Content source URL
https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/records.md#equality-members