-
Couldn't load subscription status.
- Fork 6.1k
Description
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 type T[].
For example, the type of array instance returned by RuntimeHelpers.GetSubArray<object>(new string[1], ...) is object[]
New behavior
RuntimeHelpers.GetSubArray<T>(T[] array, Range range) method returns an array instance of the same type as array argument.
For example, the type of array instance returned by RuntimeHelpers.GetSubArray<object>(new string[1], ...) is string[].
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.
- Behavioral change: Existing binaries may behave differently at run time.
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:
object[] arr = new string[1];
M(arr[1..2]);
to
string[] arr = new string[1];
M(arr[1..2]);
Feature area
Core .NET libraries
Affected APIs
System.Runtme.CompilerServices.RuntimeHelpers.GetSubArray