How to Resolve Error "CS0209: The type of a local declared in a fixed statement must be a pointer type" in C#
The Compiler Error CS0209 is a syntax error related to Unsafe Code and the fixed statement. The message reads: "The type of a local declared in a fixed statement must be a pointer type".
In C#, the fixed statement is designed specifically to "pin" a managed variable in memory so that the Garbage Collector (GC) does not move it. This pinning allows you to safely obtain a memory address. Consequently, the variable you declare inside the fixed statement parentheses must be a pointer (e.g., int*, char*, void*) to store that address. If you try to declare a regular managed type (like int, object, or string) inside the fixed statement, the compiler raises CS0209.
This guide explains the correct usage of the fixed statement and how to fix type mismatches.
Understanding the fixed Statement
The fixed keyword creates a scope where a movable variable is pinned. Its syntax is strictly defined:
fixed (PointerType* ptr = expression)
{
// ... use ptr here ...
}
PointerType*: This declares a variable that holds a memory address.expression: This usually involves the address-of operator (&) applied to a managed variable, or a string/array directly.
Because the purpose of fixed is to give you a stable memory address, the variable holding the result must be a pointer.
Scenario 1: Declaring Non-Pointer Locals
The most common cause is forgetting the asterisk * when declaring the variable inside the fixed statement.
Example of error
Trying to assign a pointer result to a regular int variable.
unsafe class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30 };
// ⛔️ Error CS0209: The type of a local declared in a fixed statement must be a pointer type
// 'int p' defines a standard integer, not a pointer.
// The 'fixed' statement requires a pointer to hold the address.
fixed (int p = numbers)
{
// ...
}
}
}
Solution: Declare a Pointer
Add the asterisk * to the type declaration to make it a pointer.
unsafe class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30 };
// ✅ Correct: 'int* p' is a pointer type.
fixed (int* p = numbers)
{
// We can now use pointer arithmetic
Console.WriteLine(*p); // 10
Console.WriteLine(*(p + 1)); // 20
}
}
}
Scenario 2: Incorrect Syntax Order
Sometimes developers confuse the fixed statement syntax with standard variable assignment or pinning handles (like GCHandle). If you try to create an object instance inside the fixed declaration part without it being a pointer assignment, CS0209 occurs.
Example of error
Attempting to declare a regular object inside the parentheses.
unsafe class Program
{
static void Main()
{
// ⛔️ Error CS0209: 'object o' is not a pointer type.
fixed (object o = new object())
{
}
}
}
Solution: Fix the Logic
If you want to pin an arbitrary object, you usually need to get a pointer to a specific field of that object, or use GCHandle if you aren't working with pointers directly.
Option A: Pinning data inside an object
class Point { public int X; }
unsafe class Program
{
static void Main()
{
Point pt = new Point();
// ✅ Correct: We pin the object by taking a pointer to one of its fields
fixed (int* p = &pt.X)
{
*p = 100;
}
}
}
Option B: Using GCHandle (Safe Context)
If you aren't using pointers (unsafe), don't use fixed. Use GCHandle.
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
object obj = new object();
// ✅ Correct: Safe way to pin an object handle
GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
try
{
// ... use handle.AddrOfPinnedObject() ...
}
finally
{
handle.Free();
}
}
}
Conclusion
CS0209 ensures you strictly follow the rules of pointer management.
- Check the Asterisk: Ensure the variable declared inside
fixed (...)has a*(e.g.,int*,char*). - Check the Context: If you are trying to pin an object without using unsafe pointers, use
GCHandleinstead of thefixedkeyword.