Created
February 2, 2018 18:04
-
-
Save richlander/8f307d410fb13a91398bc8fbb50c0aab to your computer and use it in GitHub Desktop.
Example of Span<T> and Slicing
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
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