Edit

Share via


Specifying explicit struct Size disallowed with InlineArray

Applying explicit Size to a struct decorated with InlineArrayAttribute is ambiguous and no longer supported in the type loader. Previously, specifying explicit Size would result in implementation-specific behavior that might or might not match user expectations.

Version introduced

.NET 10 Preview 7

Previous behavior

Previously, specifying Size on a struct decorated with InlineArrayAttribute was allowed, but resulted in implementation-specific behavior that might or might not match user expectations.

[InlineArray(8)]
[StructLayout(LayoutKind.Explicit, Size=32)]
struct Int8InlineArray
{
    private int _value;
}

New behavior

Starting in .NET 10, specifying Size on a struct decorated with InlineArrayAttribute is disallowed. If you attempt to create an instance of such a struct, you'll get a TypeLoadException.

Type of breaking change

This change can affect binary compatibility.

Reason for change

Specifying Size for an inline array struct is ambiguous and any interpretation would contradict the specification.

In the unlikely case you need to specify explicit size either for the array element or for the whole inline array, introduce a struct wrapping the element type or the whole array type. In the layout of the wrapper, specify Size accordingly.

Affected APIs

  • Type loader