Created
March 6, 2019 12:13
-
-
Save mgravell/1e65d60e8c21b0de18137fa39688fe25 to your computer and use it in GitHub Desktop.
This file contains 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.Text; | |
static class P | |
{ | |
static void Main() | |
{ | |
var arr = new [] | |
{ | |
new NonBlittableType(42, Encoding.UTF8.GetBytes("hello")), // 42=0x2a, for later comparison | |
new NonBlittableType(12, Encoding.UTF8.GetBytes("world")), // 12=0x0c, for later comparison | |
new NonBlittableType(05, null), // 05=0x05, for later comparison | |
}; | |
Span<NonBlittableType> span = arr; | |
ref byte root = ref Unsafe.As<NonBlittableType, byte>(ref span[0]); | |
for (int i = 0; i < span.Length * Unsafe.SizeOf<NonBlittableType>(); i++) | |
{ | |
byte b = Unsafe.Add(ref root, i); | |
Console.Write(b.ToString("x2")); | |
} | |
Console.WriteLine(); | |
} | |
readonly struct NonBlittableType | |
{ | |
public NonBlittableType(int x, byte[] obj) | |
{ | |
_x = x; | |
_obj = obj; | |
} | |
readonly int _x; | |
readonly byte[] _obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment