Last active
October 30, 2023 13:01
-
-
Save voroninp/bb91cb3626b824bd602083e5dcbdf74f to your computer and use it in GitHub Desktop.
Accessing array elements without bounds check. (from https://stackoverflow.com/a/66962968/921054)
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
public static class ArrayExtensions | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static ref T At<T>(this T[] array, int index) | |
{ | |
ref T tableRef = ref MemoryMarshal.GetArrayDataReference(array); | |
return ref Unsafe.Add(ref tableRef, (nint)index); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment