How to Resolve Error "CS0611: Array elements cannot be of type 'type'" in C#
The Compiler Error CS0611 is a memory safety restriction. The message reads: "Array elements cannot be of type 'System.TypedReference'" (or other restricted types).
In C#, arrays are objects that live on the Managed Heap. However, certain low-level types (often called "ref-like types" or "stack-only types") are designed to exist only on the Stack. Because the Heap has a longer lifespan than the Stack, storing a stack-bound type inside a heap-bound array could lead to memory corruption (dangling pointers). Therefore, the compiler forbids creating arrays of these specific types.
This guide explains which types are restricted and how to work around this limitation.
Understanding Stack-Only Types
The error applies to types that contain references to the execution stack. If these were stored in an array (which can survive after the method returns), they would point to invalid memory.
Common Restricted Types:
System.TypedReference: Used with the legacy keywords__makerefand__refvalue.System.ArgIterator: Used with the legacy__arglist.System.RuntimeArgumentHandle: Handle for variable arguments.ref structtypes (Modern C#): Examples includeSystem.Span<T>andSystem.ReadOnlySpan<T>.
Scenario 1: The TypedReference Restriction
This error often appears when developers experiment with undocumented C# keywords for dynamic typing without boxing.
Example of error: attempting to create an array to store multiple TypedReference objects.
using System;
public class MagicStorage
{
public void StoreReferences()
{
int a = 1;
int b = 2;
// ⛔️ Error CS0611: Array elements cannot be of type 'TypedReference'
// 'TypedReference' contains a pointer to the stack variable 'a'.
// The array lives on the heap. This mismatch is illegal.
TypedReference[] refs = new TypedReference[2];
// Hypothetical usage if it were allowed:
// refs[0] = __makeref(a);
}
}
Scenario 2: Modern ref struct Types (Span<T>)
With the introduction of Span<T> in modern .NET, this error is becoming more common. Span<T> is a ref struct designed to view contiguous memory. Because it can point to stack memory (stackalloc), it cannot be stored in a standard C# array (which is a heap object).
Example of error:
using System;
public class BufferManager
{
public void CreateBuffers()
{
// ⛔️ Error CS0611: Array elements cannot be of type 'Span<byte>'
// Span<T> is a stack-only type. It cannot exist inside a heap array.
Span<byte>[] buffers = new Span<byte>[10];
}
}
Solution: Use object or Memory<T>
Depending on the type causing the error, the solution varies.
Solution for TypedReference: Use object
If you are using TypedReference to store mixed types, use object[]. This causes Boxing, but it is the safe, supported way to store variables on the heap.
public class MagicStorage
{
public void StoreReferences()
{
int a = 1;
int b = 2;
// ✅ Correct: Store them as boxed objects
object[] values = new object[2];
values[0] = a;
values[1] = b;
}
}
Solution for Span<T>: Use Memory<T>
If you need an array of memory buffers, replace Span<T> with Memory<T>.
Span<T>: Stack-only, fast, synchronous access.Memory<T>: Heap-compatible, can be stored in arrays/classes, slightly slower.
using System;
public class BufferManager
{
public void CreateBuffers()
{
// ✅ Correct: Memory<T> is a struct that is NOT stack-bound.
// It can be stored in an array.
Memory<byte>[] buffers = new Memory<byte>[10];
byte[] data = new byte[100];
buffers[0] = new Memory<byte>(data, 0, 10);
}
}
You can convert a Memory<T> to a Span<T> at the moment you need to process it using .Span.
Conclusion
CS0611 prevents you from accidentally creating dangling pointers.
- Identify the Type: Is it
TypedReferenceorSpan<T>? - Understand the Limitation: These types must die when the method returns. Arrays live longer than that.
- The Fix:
- Replace
TypedReferencewithobject. - Replace
Span<T>withMemory<T>.
- Replace