Questions tagged [system.memory]
30 questions
93
votes
4 answers
What is the difference between Span and Memory in C# 7.2?
C# 7.2 introduces two new types: Span and Memory that have better performance over earlier C# types like string[].
Question: What is the difference between Span and Memory? Why would I use one over the other?

Dan Sorensen
- 11,403
- 19
- 67
- 100
59
votes
3 answers
How is the new C# Span different from ArraySegment?
I am having trouble conceptualizing the usages for the new Span in C#.
What construct(s) does it replace? Is ArraySegment now obsolete?
What functionality does it enable that previously was not?
Is Span a valid replacement for C# Arrays?…

TheFastCat
- 3,134
- 4
- 22
- 32
19
votes
6 answers
Span and two dimensional Arrays
Is it possible to use the new System.Memory Span struct with two dimensional arrays of data?
double[,] testMulti =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 9.5f, 10, 11 },
{ 12, 13, 14.3f, 15 }
};
double[]…

Mick
- 6,527
- 4
- 52
- 67
15
votes
3 answers
Span does not require local variable assignment. Is that a feature?
I notice that the following will compile and execute even though the local variables are not initialized. Is this a feature of Span?
void Uninitialized()
{
Span s1;
var l1 = s1.Length;
Span s2;
UninitializedOut(out s2);
var l2…

jyoung
- 5,071
- 4
- 30
- 47
12
votes
1 answer
Are readonly structs supposed to be immutable when in an array?
(Note: This sample code requires C# 7.2 or later, and the Nuget System.Memory package.)
Let's suppose we have a readonly struct as follows:
public readonly struct Test
{
public Test(int value)
{
Value = value;
}
public int…

Matthew Watson
- 104,400
- 10
- 158
- 276
9
votes
1 answer
Using Span as a replacement for Substring
I have read a few articles about how Span can be used to replace certain string operations. As such I have updated some code in my code base to use this new feature, however, to be able to use it in-place I then have to call .ToString().
Does the…

Chris
- 26,744
- 48
- 193
- 345
7
votes
1 answer
Which common operations can be made more efficient by Span?
Let's say I have a Web application and I want to make use of the new Span type to reduce GC pressure and improve performance.
Which patterns should I look out for? Are there any typical operations that the .NET team had in mind when implementing…

Kirill Rakhman
- 42,195
- 18
- 124
- 148
6
votes
1 answer
How can I do operations on Span in parallel?
I want to do operations on Span in parallel but something like this is not legal:
void DoSomething(Span buffer, int option1, int option2)
{
.......
}
void ParallelDoSomething(Span buffer)
{
var size = buffer.Length;
…

Vlad Radu
- 337
- 1
- 13
6
votes
1 answer
Span and friends not working in .NET Native UWP app
Steps to reproduce:
Open Visual Studio 2017 with latest update.
Create a UWP project targetin 10240 (this is not mandatory, it's broken in all builds)
Install System.Memory from nuget packages (click include prerelease)
Copy paste this code in to…

Fritjof Berggren
- 3,178
- 5
- 35
- 57
5
votes
0 answers
Find position of Span in an array
Span points to a memory area. Given that I know what memory area it is (i.e. array), can I (efficiently) figure out what first element in the array it is pointing to? I figured since it is pointing to non-pinned memory this information has to be…

Tedd Hansen
- 12,074
- 14
- 61
- 97
5
votes
1 answer
What is a working alternative to being unable to pass a Span into lambda expression?
I have a function of this kind of shape which does 1 dimensional rootfinding:
public delegate double Fun(double x, object o);
public static void Solve(Fun f, out double y, object o)
{
y = f(1.0, o); // all the irrelevant details of the…

lamont
- 3,854
- 1
- 20
- 26
5
votes
1 answer
Proper way to get a mutable struct for Memory / Span?
For a network protocol implementation I want to make use of the new Memory and Span classes to achieve zero-copy of the buffer while accessing the data through a struct.
I have the following contrived example:
[StructLayout(LayoutKind.Sequential,…

mycroes
- 645
- 8
- 20
5
votes
1 answer
Substring implementation via Span
How would an implementation of a SubstringFromStart method look like when Span should be leveraged? Assuming substringLength <= input.Length:
ReadOnlySpan span = input.AsSpan().Slice(0, substringLength);
return new…

David
- 2,426
- 3
- 24
- 36
4
votes
1 answer
Struct to it's ReadOnlyMemory representation
I have following struct:
[StructLayout(LayoutKind.Sequential)]
struct Message
{
int Header;
int Data;
}
and I want to send it over the wire, without allocations (using SendAsync(ReadOnlyMemory)) call.
How can I get the Memory…

nothrow
- 15,882
- 9
- 57
- 104
4
votes
1 answer
Alternative to using Func to return a Span or other ref struct
How can one pass as a parameter a method that returns a Span?
using System.Memory;
public Span CallSpanFactory1(Func> spanFactory)
{
return spanFactory();
}
This code returns the error "the type 'Span' may not be used as a…

mbabramo
- 2,573
- 2
- 20
- 24