Created
April 14, 2017 16:34
-
-
Save svick/0dd2398c193d4f2450e9c19a3a020c63 to your computer and use it in GitHub Desktop.
https://github.com/dotnet/coreclr/issues/10449 StructLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Runtime.CompilerServices; | |
| using System.Runtime.InteropServices; | |
| class Program | |
| { | |
| static unsafe void Main() | |
| { | |
| Console.WriteLine($"Sequential: {sizeof(SequentialS)}"); | |
| Console.WriteLine($"Auto: {sizeof(AutoS)}"); | |
| Console.WriteLine(ToTuple(new AutoS())); | |
| Console.WriteLine(ToTuple(new SequentialS())); | |
| } | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static (short, short, short, DateTime, short) ToTuple(SequentialS s) => | |
| (s._a, s._b, s._c, s._dateTimeOffset._dateTime, s._dateTimeOffset._offsetMinutes); | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static (short, short, short, DateTime, short) ToTuple(AutoS s) => | |
| (s._a, s._b, s._c, s._dateTimeOffset._dateTime, s._dateTimeOffset._offsetMinutes); | |
| } | |
| [StructLayout(LayoutKind.Auto)] | |
| struct MyDateTimeOffset | |
| { | |
| public DateTime _dateTime; | |
| public short _offsetMinutes; | |
| } | |
| [StructLayout(LayoutKind.Sequential)] | |
| struct SequentialS | |
| { | |
| public short _a; | |
| public short _b; | |
| public short _c; | |
| public MyDateTimeOffset _dateTimeOffset; | |
| } | |
| [StructLayout(LayoutKind.Auto)] | |
| struct AutoS | |
| { | |
| public short _a; | |
| public short _b; | |
| public short _c; | |
| public MyDateTimeOffset _dateTimeOffset; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment