- Span<T> provides a type-safe and memory-safe representation of a contiguous region of arbitrary memory.
- Span<T> is a lightweight, stack-allocated (rather than heap) alternative to the array type and can be used to avoid unnecessary memory allocation and garbage collection.
- Span<T> has several useful methods for manipulating data, such as Slice and CopyTo, which allow for easy manipulation of subarrays.
- The main advantage of using Span<T> is that it allows for more efficient memory management, as it does not require allocating new memory when working with large data sets. Additionally, Span<T> also provides a number of safety features, such as bounds checking, to help prevent common programming errors.
- For spans that represent immutable or read-only structures, use System.ReadOnlySpan<T>
Read more here
Span Struct (System)
Provides a type-safe and memory-safe representation of a contiguous region of arbitrary memory. generic public value class Span public readonly ref struct Span [System.Runtime.InteropServices.Marshalling.NativeMarshalling(typeof(System.Runtime.InteropServices.Marshalling.SpanMarshaller ))] public readonly ref struct Span type Span = struct [ ))>] type Span = struct Public Structure Span(Of T) Type Parameters The type of items in the Span .
What is Span in C# and why you should be using it
Become a Patreon and get source code access: https://www.patreon.com/nickchapsas Check out my courses: https://nickchapsas.com Hello everybody I'm Nick and in this video I am going to talk about Span of T in C#. Span was introduced in C# and .NET alongside a series of optimisations all the way back in .NET 2.1 and it has come to my attention that it is really confusing to understand if you just read the documentation for it. In this video I will break it down for you and help you understand exactly what it is, how it works and how you can use it in your applications today! Don't forget to comment, like and subscribe :) Social Media: Follow me on GitHub: http://bit.ly/ChapsasGitHub Follow me on Twitter: http://bit.ly/ChapsasTwitter Connect on LinkedIn: http://bit.ly/ChapsasLinkedIn Keep coding merch: https://keepcoding.shop #csharp #dotnet #span