Here is where a C# span can and cannot be #shorts



Here is where a C# span can and cannot be #shorts

Here is where a C# span can and cannot be #shorts

Spans are a new hot topic in C# when optimizing CPU-bound code. But did you know that most of the span’s incredible performance comes from the critical constraint put upon it. The span can only exist on the stack.
And so there can never exist an array of spans. Nor any other collection of spans for that matter.
You cannot capture it in a closure.
A span cannot be boxed, so forget assigning it to a reference.
A class cannot have a field of type span.
What can it be, then? Well, it is possible to hold a common variable which is a span, obviously.
And you can also include it as a field inside other ref structs. Any ref struct must entirely reside on stack, so this is fine.
It can also be a method’s argument. Arguments are passed via stack anyway. It can be the method’s return type as well.

Video courses:
Beginning Object-oriented Programming with C# ► https://codinghelmet.com/go/beginning-oop-with-csharp
Collections and Generics in C# ► https://codinghelmet.com/go/collections-and-generics-in-cs
Making Your C# Code More Object-oriented ► https://codinghelmet.com/go/making-your-cs-code-more-oo

Other courses at Pluralsight ► https://codinghelmet.com/go/pluralsight
Other courses at Udemy ► https://codinghelmet.com/go/udemy

Additional videos:
The Fastest Way to Modify a List in C# ► https://youtu.be/0oSN65eM-tc
Coding with GitHub Copilot – Beginner to Master ► https://youtu.be/B9DKv09IfT4
Want to Start with DDD? Try Ubiquitous Language First! ► https://youtu.be/_zWMjMUHinc
Using C# Record Types ► https://youtu.be/VouNkrgkH78
Covariance and Contravariance in C# ► https://youtu.be/Wp5iYQqHspg
How do Virtual Functions Work? ► https://youtu.be/b0IDTWoaUJ0

Practical Design playlist at YouTube ► https://youtube.com/playlist?list=PLSDYwLgFqaX78eEg_YKs0wHmOMi7_ggZr

Comments are closed.