Home
Map
Span ExamplesUse the Span type and call methods like Fill and ToArray to change values in a region of memory.
C#
This page was last reviewed on May 13, 2023.
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 usage. Span can be used with unsafe code for performance optimizations. It can also be used with arrays to perform low-level logic.
Array
Fill. 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
Slice. 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.
Part 1 We invoke Slice on the initial Span with 1 argument—this span starts at index 1 and covers the rest of the elements.
Part 2 We call Slice with 2 arguments. Argument 1 is the start index, and argument 2 is the desired length.
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
Pointer. 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.
Part 1 We use the fixed keyword to assign to elements in the fixed buffer. We assign 3 elements.
fixed
Part 2 We create a Span from the fixed buffer struct. We then use the foreach-loop on the Span to print the elements to the screen.
foreach
Console.WriteLine
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
Empty. 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.
String ToCharArray
char Array
Then We can call 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 13, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.