Skip to content

Document InlineArrayAttribute is the correct approach for fixed-size buffers#50898

Merged
AaronRobinsonMSFT merged 7 commits into
mainfrom
copilot/implement-user-suggestion-from-issue
Jul 7, 2026
Merged

Document InlineArrayAttribute is the correct approach for fixed-size buffers#50898
AaronRobinsonMSFT merged 7 commits into
mainfrom
copilot/implement-user-suggestion-from-issue

Conversation

Copilot AI commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

Fixes dotnet/runtime#112104

Users attempting to use StructLayoutAttribute.Size to create fixed-size buffers encounter memory corruption because the Size property only affects unmanaged layout, not managed memory allocation for non-blittable types.

Changes

  • customize-struct-marshalling.md: Added "Fixed-size buffers and inline arrays" section

    • Warns against using StructLayoutAttribute.Size for fixed-size buffers
    • Explains technical limitation: only affects managed layout for blittable types
    • Provides dangerous vs. safe example using InlineArrayAttribute
    • Cross-references C# language documentation
  • apply-interop-attributes.md: Added cross-reference to InlineArrayAttribute in the StructLayoutAttribute table entry

Example

// ❌ Dangerous: only allocates 1 byte, corrupts memory beyond
[StructLayout(LayoutKind.Explicit, Size = 100)]
struct UnsafeBuffer
{
    [FieldOffset(0)]
    private byte b;
}

// ✔️ Safe: allocates full 100 bytes
struct SafeBuffer
{
    private Buffer100 buffer;
}

[InlineArray(100)]
internal struct Buffer100
{
    private byte _element0;
}
Original prompt

Please implement the suggestion from the user in the last common on this page: dotnet/runtime#112104


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/standard/native-interop/apply-interop-attributes.md Applying Interop Attributes
docs/standard/native-interop/customize-struct-marshalling.md Customizing structure marshalling - .NET

Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement user suggestion from issue 112104 Document InlineArrayAttribute as the correct approach for fixed-size buffers Jan 6, 2026
Copilot AI requested a review from agocke January 6, 2026 00:23
@AaronRobinsonMSFT AaronRobinsonMSFT marked this pull request as ready for review July 7, 2026 17:56
Copilot AI review requested due to automatic review settings July 7, 2026 17:56
@AaronRobinsonMSFT AaronRobinsonMSFT requested review from a team and jkoritzinsky as code owners July 7, 2026 17:56
@github-actions github-actions Bot changed the title Document InlineArrayAttribute as the correct approach for fixed-size buffers Document InlineArrayAttribute ██ the correct approach for fixed-size buffers Jul 7, 2026
@AaronRobinsonMSFT

AaronRobinsonMSFT commented Jul 7, 2026

Copy link
Copy Markdown
Member

/cc @gewarren @jkotas @elinor-fung

@github-actions github-actions Bot added profane content 🤬 Either the title or body text contained profanity and removed dotnet-fundamentals/svc labels Jul 7, 2026
@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Document InlineArrayAttribute ██ the correct approach for fixed-size buffers Document InlineArrayAttribute as the correct approach for fixed-size buffers Jul 7, 2026
@AaronRobinsonMSFT AaronRobinsonMSFT added dotnet-fundamentals/svc and removed profane content 🤬 Either the title or body text contained profanity labels Jul 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the native interop documentation to steer readers away from using StructLayoutAttribute.Size as a managed fixed-size buffer mechanism, and to point them to C# inline arrays (InlineArrayAttribute) as the correct approach.

Changes:

  • Added a new “Fixed-size buffers and inline arrays” section to explain why StructLayoutAttribute.Size can be unsafe for managed fixed-size buffers and to recommend InlineArrayAttribute.
  • Updated the interop attributes reference table to cross-reference InlineArrayAttribute from the StructLayoutAttribute entry.
Show a summary per file
File Description
docs/standard/native-interop/customize-struct-marshalling.md Adds guidance and examples for fixed-size buffers, recommending inline arrays over StructLayoutAttribute.Size.
docs/standard/native-interop/apply-interop-attributes.md Updates the StructLayoutAttribute table entry to point readers toward inline arrays for fixed-size buffers.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 3

Comment thread docs/standard/native-interop/customize-struct-marshalling.md
Comment thread docs/standard/native-interop/customize-struct-marshalling.md
Comment thread docs/standard/native-interop/apply-interop-attributes.md Outdated
@github-actions github-actions Bot changed the title Document InlineArrayAttribute as the correct approach for fixed-size buffers Document InlineArrayAttribute ██ the correct approach for fixed-size buffers Jul 7, 2026
@github-actions github-actions Bot added profane content 🤬 Either the title or body text contained profanity and removed dotnet-fundamentals/svc labels Jul 7, 2026
Comment thread docs/standard/native-interop/customize-struct-marshalling.md Outdated
AaronRobinsonMSFT and others added 2 commits July 7, 2026 11:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Document InlineArrayAttribute ██ the correct approach for fixed-size buffers Document InlineArrayAttribute is the correct approach for fixed-size buffers Jul 7, 2026
@AaronRobinsonMSFT AaronRobinsonMSFT added dotnet-fundamentals/svc and removed profane content 🤬 Either the title or body text contained profanity labels Jul 7, 2026
Comment thread docs/standard/native-interop/customize-struct-marshalling.md Outdated
@AaronRobinsonMSFT AaronRobinsonMSFT self-requested a review July 7, 2026 18:43
Comment thread docs/standard/native-interop/customize-struct-marshalling.md
@AaronRobinsonMSFT AaronRobinsonMSFT merged commit fd1fbf2 into main Jul 7, 2026
11 checks passed
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the copilot/implement-user-suggestion-from-issue branch July 7, 2026 19:30
Comment thread docs/standard/native-interop/customize-struct-marshalling.md
Comment thread docs/standard/native-interop/customize-struct-marshalling.md
Comment thread docs/standard/native-interop/customize-struct-marshalling.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Runtime.InteropServices.StructLayoutAttribute.Size behavior on reference types

6 participants