Created
April 28, 2019 14:33
-
-
Save Zonciu/08e2e63ad3c17908862a9e80e9dc449b to your computer and use it in GitHub Desktop.
Fixed length array, available for unmanaged type
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
[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}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test passed in .NET Framework, .NET Core, Unity Android(IL2CPP, Mono), Xenko Android, Xamarin Android.