Interview Question

What is Span<T>?

Span<T> is a non-owning, stack-only view over contiguous memory.

💡 Concept ✅ Quick Revision 🔷 C#

Answer

Span<T> is a stack-only ref struct that represents a contiguous region of arbitrary memory. • It can point into arrays, stack memory, strings through ReadOnlySpan<char>, or unmanaged memory. • Slicing creates another view without copying the elements. • Because it is stack-only, Span<T> cannot be boxed or stored in ordinary heap objects and cannot cross an await boundary.

💡 C# Example

int[] values = [10, 20, 30, 40]; Span<int> middle = values.AsSpan(1, 2); middle[0] = 99; Console.WriteLine(values[1]);

Output

99

⚡ Quick Revision

Span<T> is a non-owning, stack-only view over contiguous memory.