Skip to content

Instantly share code, notes, and snippets.

@richlander
Created February 2, 2018 18:04
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 richlander/8f307d410fb13a91398bc8fbb50c0aab to your computer and use it in GitHub Desktop.
Save richlander/8f307d410fb13a91398bc8fbb50c0aab to your computer and use it in GitHub Desktop.
Example of Span<T> and Slicing
Span<byte> slicedBytes = bytes.Slice(start: 5, length: 2);
slicedBytes[0] = 42;
slicedBytes[1] = 43;
Assert.Equal(42, slicedBytes[0]);
Assert.Equal(43, slicedBytes[1]);
Assert.Equal(arr[5], slicedBytes[0]);
Assert.Equal(arr[6], slicedBytes[1]);
slicedBytes[2] = 44; // Throws IndexOutOfRangeException
bytes[2] = 45; // OK
Assert.Equal(arr[2], bytes[2]);
Assert.Equal(45, arr[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment