[Breaking change]: Change the type of array instance returned RuntimeHelpers.GetSubArray to match the source array #38474
Labels
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 9
Work items for the .NET 9 release
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Pri1
High priority, do before Pri2 and Pri3
📌 seQUESTered
Identifies that an issue has been imported into Quest.
Description
The type of array instance returned by
RuntimeHelpers.GetSubArray
is changed to match the source array.RuntimeHelpers.GetSubArray
is used by C# compiler to implement range operator for arrays.This behavior change can be only observable by code that uses covariant array conversions.
Version
.NET 9 Preview 1
Previous behavior
RuntimeHelpers.GetSubArray<T>(T[] array, Range range)
method returns an array instance of typeT[]
.For example, the type of array instance returned by
RuntimeHelpers.GetSubArray<object>(new string[1], ...)
isobject[]
New behavior
RuntimeHelpers.GetSubArray<T>(T[] array, Range range)
method returns an array instance of the same type asarray
argument.For example, the type of array instance returned by
RuntimeHelpers.GetSubArray<object>(new string[1], ...)
isstring[]
.Type of breaking change
Reason for change
The design of C# pattern matching features assume that the type of array instance returned RuntimeHelpers.GetSubArray match the source array. The previous behavior led to unexpected behavior of certain complex pattern expressions that used slicing of co-variant arrays. See dotnet/roslyn#69053 for details.
Recommended action
The recommended action is to remove dependency of the affected code on array co-variance.
For example, change:
to
Feature area
Core .NET libraries
Affected APIs
System.Runtme.CompilerServices.RuntimeHelpers.GetSubArray
Associated WorkItem - 196551
The text was updated successfully, but these errors were encountered: