Span<T> in C# (Interview Guide)

Most developers say:
“It is a faster array.”
That’s wrong — and will fail a senior interview.

What is Span<T>?

❌ Bad answer

It is a faster array.

✅ Senior answer

Span<T> is a lightweight struct that provides safe access to contiguous memory without allocations. It can point to stack memory, arrays, or unmanaged memory, helping reduce allocations and improve performance.

How Span works

Example

int[] arr = {1, 2, 3, 4};

Span<int> slice = arr.AsSpan(1, 2);
// slice = {2, 3}

Why it matters (Performance)

Span avoids allocations → less GC → better performance in high-throughput systems like APIs and real-time processing.

Common mistakes

❌ Trying to store Span in class fields

Span must stay on the stack → cannot be stored in heap objects.

❌ Overusing Span

Adds complexity when not needed.

Want to master .NET performance like a senior?

Practice Now
← See all .NET interview questions