You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Structs that don't contain reference types (directly nor indirectly) have the Sequential layout by default. This is to prevent breaking any project that uses interop code and requires that the fields stay ordered. In other cases however, field reordering is usually fine and there are little reasons to use the Sequential layout over the Automatic one, as it will optimize the layout for you, potentially saving memory and improving performance in the process.
It can be ignored if you know you require the Sequential layout for all of your structs, most likely for interop purposes.
Examples
publicstructMyStruct// Compliant, no field{}publicstructMyStruct// Compliant, only one field, no ordering possible{publicintA;}publicstructMyStruct// Non compliant, specify the layout. Will take 24 bytes because of Sequential{publicintA;publicdoubleB;publicintC;}[StructLayout(LayoutKind.Sequential)]publicstructMyStruct// Compliant, specified layout. Will take 24 bytes because of Sequential{publicintA;publicdoubleB;publicintC;}[StructLayout(LayoutKind.Auto)]publicstructMyStruct// Compliant, specified layout. Will take 16 bytes thanks to Auto{publicintA;publicdoubleB;publicintC;}publicstructMyStruct// Compliant, string is a reference type, LayoutKind.Auto will be forced. Will take 16 bytes{publicintA;publicstringB;publicintC;}
The text was updated successfully, but these errors were encountered:
Category : Performance
Severity : Warning
Why is this an issue ?
Structs that don't contain reference types (directly nor indirectly) have the
Sequential
layout by default. This is to prevent breaking any project that uses interop code and requires that the fields stay ordered. In other cases however, field reordering is usually fine and there are little reasons to use theSequential
layout over theAutomatic
one, as it will optimize the layout for you, potentially saving memory and improving performance in the process.You can read a much more in-depth analysis of struct packing here : http://www.catb.org/esr/structure-packing/
When can it be ignored ?
It can be ignored if you know you require the
Sequential
layout for all of your structs, most likely for interop purposes.Examples
The text was updated successfully, but these errors were encountered: