Skip to content

Instantly share code, notes, and snippets.

@Zonciu
Created April 28, 2019 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zonciu/08e2e63ad3c17908862a9e80e9dc449b to your computer and use it in GitHub Desktop.
Save Zonciu/08e2e63ad3c17908862a9e80e9dc449b to your computer and use it in GitHub Desktop.
Fixed length array, available for unmanaged type
[StructLayout(LayoutKind.Sequential)]
public struct FixedArray8<T>
where T : unmanaged
{
public T Value0;
public T Value1;
public T Value2;
public T Value3;
public T Value4;
public T Value5;
public T Value6;
public T Value7;
public ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (index > -1 && index < 8)
{
unsafe
{
fixed (T* ptr = &Value0)
{
return ref *(ptr + index);
}
}
}
throw new IndexOutOfRangeException($"{nameof(FixedArray8<T>)} index can't be {index}");
}
}
}
@Zonciu
Copy link
Author

Zonciu commented Apr 28, 2019

Test passed in .NET Framework, .NET Core, Unity Android(IL2CPP, Mono), Xenko Android, Xamarin Android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment