Span
This is a generic C# type that can be used to act upon a region of memory. Span
provides methods for testing, changing, and converting elements in memory.
Span
usageSpan
can be used with unsafe code for performance optimizations. It can also be used with arrays to perform low-level logic.
This program creates a Span
of ints by passing an int
array to the Span
constructor. It then calls fill to change all elements to the value 5.
using System; Span<int> span = new Span<int>(new int[] { 1, 2, 3 }); // Fill span with value 5. span.Fill(5); foreach (int value in span) { Console.WriteLine(value); }5 5 5
ToArray
We can create a Span
from a same-typed array. And in the same way, we can call ToArray
to convert a span into an array of the same type.
using System; var animals = new string[] { "bird", "frog", "dog" }; Span<string> span = new Span<string>(animals); // Use ToArray to convert a span into an array. string[] result = span.ToArray(); Console.WriteLine("RESULT: {0}", string.Join(",", result)); Console.WriteLine("RESULT LENGTH: {0}", result.Length);RESULT: bird,frog,dog RESULT LENGTH: 3
The Slice()
method is like Substring
for Spans—it receives a start index and an optional length as its arguments. It returns a new Span
.
Span
with 1 argument—this span starts at index 1 and covers the rest of the elements.using System; Span<int> span = new Span<int>(new int[] { 1, 2, 3 }); // Part 1: take slice at index 1 through the end. var slice1 = span.Slice(1); Console.WriteLine(string.Join(",", slice1.ToArray())); // Part 2: take slice at index 0 with length 2. var slice2 = span.Slice(0, 2); Console.WriteLine(string.Join(",", slice2.ToArray()));2,3 1,2
We can create a Span
from a pointer to memory in .NET Core. In this example, we use a fixed buffer as the pointer. We must specify the length of the span.
Span
from the fixed buffer struct
. We then use the foreach
-loop on the Span
to print the elements to the screen.using System; unsafe struct FixedBufferExample { public fixed int _buffer[1024]; } class Program { static FixedBufferExample _fixedBufferExample; static void Main() { unsafe { // Part 1: store values in fixed buffer. fixed (int* buffer = _fixedBufferExample._buffer) { buffer[0] = 10; buffer[1] = 20; buffer[2] = 30; // Part 2: create span from fixed buffer struct. Span<int> span = new Span<int>(buffer, 3); foreach (int value in span) { Console.WriteLine("BUFFER ELEMENT: {0}", value); } } } } }BUFFER ELEMENT: 10 BUFFER ELEMENT: 20 BUFFER ELEMENT: 30
To create an empty span, we can use the Span.Empty
property. We must specify a type parameter on the Empty property. The resulting span has a length of 0.
using System; Span<char> span = Span<char>.Empty; Console.WriteLine("EMPTY LENGTH: {0}", span.Length);EMPTY LENGTH: 0
ToString
Here we create a span of chars from a char
array. We call ToCharArray
to get a char
array from a string
. With a span, we can modify elements by assigning them.
ToString
to convert a span into its string
representation. For a char
Span
, this is a useful method.using System; Span<char> span = new Span<char>("bird".ToCharArray()); span[1] = 'a'; string result = span.ToString(); Console.WriteLine("RESULT: {0}", result);RESULT: bard
Contains
Much like a string
, we can use Contains
to search a span. With a Span
of chars, we can find a specific char
—here we find the char
"c" in the Span
.
using System; Span<char> span = new Span<char>(new char[] { 'a', 'b', 'c' }); // Use Contains to search a span. if (span.Contains('c')) { Console.WriteLine("SPAN CONTAINS C"); }SPAN CONTAINS C
Span
can receive an array, or a pointer to a region of memory. We can use string
-like methods on Span
like Contains
or Slice. And we can loop with foreach
over a Span
.