Created
December 12, 2021 09:04
-
-
Save nishanc/e5311dd00af80f6b08fe5ac4a75817a7 to your computer and use it in GitHub Desktop.
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
string[] array = { "a", "b", "c", "d", "e" }; | |
// Using Span ctor (array, start, length) | |
// Note that the spans overlap | |
var firstView = new Span<string>(array, 0, 3); | |
PrintArray(firstView); | |
var secondView = new Span<string>(array, 2, 3); | |
PrintArray(secondView); | |
firstView[0] = "w"; | |
PrintArray(array); | |
// array = { "w", "b", "c", "d", "e" } | |
firstView[2] = "x"; | |
PrintArray(array); | |
// array = { "w", "b", "x", "d", "e" } | |
secondView[0] = "y"; | |
PrintArray(array); | |
// array = { "w", "b", "y", "d", "e" } | |
// Throws IndexOutOfRangeException | |
// firstView[4] = "a"; | |
void PrintArray(Span<string> array) | |
{ | |
foreach (string? item in array) | |
{ | |
Console.Write($"{item}, "); | |
} | |
Console.WriteLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment