Skip to content

Instantly share code, notes, and snippets.

@voroninp
Last active October 30, 2023 13:01
Show Gist options
  • Save voroninp/bb91cb3626b824bd602083e5dcbdf74f to your computer and use it in GitHub Desktop.
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)
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