Span<T> in C#. .NET Core

Span is a new type we are adding to the platform to represent contiguous regions of arbitrary memory, with performance characteristics on par with T[]. Its APIs are similar to the array, but unlike arrays, it can point to either managed or native memory, or to memory allocated on the stack.

Sa inteleg ca este o zona continua de memorie ?

Its APIs are similar to the array, but unlike arrays, it can point to either managed or native memory, or to memory allocated on the stack.

The greatest benefit of Span<T> in C# is that it allows you to do things that once required an unsafe context in an efficient, (memory) safe, and type-safe manner.

The Github link has an example:

  Span<byte> stackSpan;
  unsafe {
      byte* stackMemory = stackalloc byte[100];
      stackSpan = new Span<byte>(stackMemory, 100);
  }
  SafeSum(stackSpan);

You still need unsafe to allocate, but once allocated, you can access the memory in a type safe way.

1 Like

1 Like