Home

Search

C# Span Examples

Use the Span type and call methods like Fill and ToArray to change values in a region of memory.

dot net perls

Span. This is a generic type that can be used to act upon a region of memory. Span provides methods for testing, changing, and converting elements in memory.

As part of .NET Core, 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.
C# program that uses Span using System; class Program { static void Main() { 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); } } } Output 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.
C# program that uses ToArray using System; class Program { static void Main() { 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); } } Output 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.
C# program that uses Slice using System; class Program { static void Main() { 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())); } } Output 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.
ForeachConsole
C# program that uses pointer with Span 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); } } } } } Output 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.
C# program that uses Span.Empty using System; class Program { static void Main() { Span<char> span = Span<char>.Empty; Console.WriteLine("EMPTY LENGTH: {0}", span.Length); } } Output 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.ToCharArrayChar Array
Then: We can call ToString to convert a span into its string representation. For a char Span, this is a useful method.
C# program that uses Span ToString using System; class Program { static void Main() { Span<char> span = new Span<char>("bird".ToCharArray()); span[1] = 'a'; string result = span.ToString(); Console.WriteLine("RESULT: {0}", result); } } Output 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.
C# program that uses Contains method using System; class Program { static void Main() { 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"); } } } Output SPAN CONTAINS C

A summary. 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.

Home

© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.