docs icon indicating copy to clipboard operation
docs copied to clipboard

[Breaking change]: Default `Equals()` and `GetHashCode()` throw for types marked with `InlineArrayAttribute`.

Open AaronRobinsonMSFT opened this issue 1 year ago • 1 comments

Description

The default behavior for Equals() and GetHashCode() on types marked with InlineArrayAttribute is now to throw a NotSupportedException. The user is required to override those methods if these functions are expected to not throw.``

Version

.NET 9 Preview 6

Previous behavior

The previous default implementation will only use the placeholder ref field when computing equality or the hash code.

New behavior

A NotSupportedException will always be thrown from the default implementations for Equals() and GetHashCode() when InlineArrayAttribute is applied to a type.

Type of breaking change

  • [ ] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • [ ] Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • [X] Behavioral change: Existing binaries may behave differently at run time.

Reason for change

The current behavior is not correct for either determining equality or hash code. Users are led into a false sense of correctness when calling these functions currently.

Recommended action

Users should implement both Equals() and GetHashCode() on all types marked with InlineArrayAttribute.

Feature area

Core .NET libraries

Affected APIs

System.ValueType.Equals(object) and System.ValueType.GetHashCode()

AaronRobinsonMSFT avatar Jun 20 '24 18:06 AaronRobinsonMSFT

Official API documentation is being updated with the new behavior - https://github.com/dotnet/dotnet-api-docs/pull/10036

AaronRobinsonMSFT avatar Jun 20 '24 18:06 AaronRobinsonMSFT