How to Resolve Error "CS0610: Field or property cannot be of type 'type'" in C#
The Compiler Error CS0610 is a restriction error related to Stack-Only Types. The message reads: "Field or property cannot be of type 'System.TypedReference'" (or System.ArgIterator, System.RuntimeArgumentHandle).
In C#, certain special types are designed to point directly to memory locations on the execution stack. Because stack frames are temporary (they are destroyed when a method returns), storing references to them in a class field (which lives on the heap) would lead to memory corruption or invalid pointers. Therefore, the compiler strictly enforces that these types can only exist as local variables or parameters.
This guide explains which types cause this error and how to handle them correctly.
Understanding Restricted Types
The types that trigger CS0610 are part of the low-level, often undocumented "magic" features of C# used for interoperability or dynamic typing before dynamic existed.
The Prohibited Types:
System.TypedReference: Created using the keyword__makeref. It holds a pointer to a variable and its type.System.ArgIterator: Used to iterate over__arglistvariable arguments.System.RuntimeArgumentHandle: A handle to the argument list.
These types are ref-like constructs. You cannot store them in a class because the class instance might live longer than the stack frame the reference points to.
Modern Note: This error is distinct from the error for ref struct types (like Span<T>). Putting a Span<T> in a class field triggers CS8345, whereas TypedReference triggers CS0610.
Scenario: Storing a TypedReference in a Field
This error usually appears when developers are experimenting with the undocumented keywords __makeref, __refvalue, or __reftype and attempt to cache the result.
Example of error:
using System;
public class MagicContainer
{
// ⛔️ Error CS0610: Field or property cannot be of type 'TypedReference'
// You cannot store a stack reference on the heap.
public TypedReference Ref;
public void SetData(int data)
{
// __makeref creates a TypedReference to 'data'
Ref = __makeref(data);
}
}
Solution: Use Local Variables
You must restrict the usage of these types to the method scope. Use them, extract the data you need, and then let them go out of scope.
Solution
Keep the TypedReference inside the method.
using System;
public class MagicContainer
{
// Store the actual value (int, object, etc.), not the reference.
public int StoredValue;
public void ProcessData(int data)
{
// ✅ Correct: TypedReference is valid as a local variable
TypedReference tr = __makeref(data);
// Use it immediately
Console.WriteLine(__refvalue(tr, int));
// If you need to store it, store the value, not the reference
StoredValue = __refvalue(tr, int);
}
}
Alternative Solution: Boxing
If you are trying to store a reference to "anything," use object. While less performant than TypedReference, it is safe and allowed in fields.
public class Container
{
// ✅ Correct: 'object' stores a copy (boxed) on the heap
public object Data;
}
Conclusion
CS0610 is a memory safety mechanism.
- Identify the Type: Are you using
TypedReference,ArgIterator, orRuntimeArgumentHandle? - Check the Scope: Are you trying to declare it as a
public/privatefield or property? - The Rule: These types must die when the method returns. Keep them as local variables.