Skip to content

Instantly share code, notes, and snippets.

@mgravell
Created March 6, 2019 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgravell/1e65d60e8c21b0de18137fa39688fe25 to your computer and use it in GitHub Desktop.
Save mgravell/1e65d60e8c21b0de18137fa39688fe25 to your computer and use it in GitHub Desktop.
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