In C#, GetPinnableReference() returns a read-only reference (ref readonly char) to the first character of a string. It exists so that the fixed statement can pin the string: tell the garbage collector not to move it while a pointer points into it.
Honest summary: you almost never call this method yourself. Since C# 7.3, any type with a GetPinnableReference() method can be used in a fixed statement, and string provides one for that pattern. Everyday code reads characters with text[i], foreach, or text.AsSpan(), which are safe and just as fast.
This is an instance member of System.String. Call it on a string variable, such as text.GetPinnableReference(...). Calling it on a null variable throws NullReferenceException. It only reads the text. Strings are immutable, so the original string is never changed. It exists in .NET Core 3.0 and later. It is meant for compilers and very low-level code; normal code never calls it.
Syntax
You can call it directly into a ref readonly local, but its real job is behind fixed.
C#
string text = "Hello";
ref readonly char first = ref text.GetPinnableReference(); // 'H' (no copy)
// In unsafe code (needs AllowUnsafeBlocks), fixed pins the string:
// fixed (char* p = text) { ... p[0] is 'H' ... }
Call
What it does
ref readonly char c = ref text.GetPinnableReference();
A read-only reference to text[0] (rarely needed).
fixed (char* p = text) { ... }
Pin the string and get a pointer to its first char (unsafe code).
ReadOnlySpan<char> s = text.AsSpan();
The safe, everyday way to work with the characters without copying.
Unsafe code requires <AllowUnsafeBlocks>true</AllowUnsafeBlocks> in the project file (or /unsafe). Never write through a pointer into a string: strings are immutable, and other code (including the string pool) may share the same object. For an empty string, the reference points at the hidden '\0' that .NET keeps after every string's characters.
Library declarations
C#
public ref readonly char GetPinnableReference(); // .NET Core 3.0+
Parameters
Parameter
Description
None
GetPinnableReference() takes no parameters.
Return Value
Return Value
Description
ref readonly char
A reference to the first character (or to the terminating '\0' for an empty string). You can read it, not change it.
C# Examples
Example 1 — Read the First Character Through a Reference
Call the method directly in safe code and read what the reference points to, for a normal and an empty string.
C#
using System;
class Program {
static void Main() {
string text = "Hello";
ref readonly char first = ref text.GetPinnableReference();
Console.WriteLine(first);
string empty = "";
ref readonly char end = ref empty.GetPinnableReference();
Console.WriteLine((int)end);
}
}
Output
H
0
How It Works
A reference, not a copy.ref readonly char refers to the character stored inside the string; no new value or string is created.
The first character. For Hello, the reference points at H.
Read-only on purpose. Because of readonly, writing first = 'J'; would not compile, which protects the immutable string.
Empty strings still have a target. .NET stores a '\0' after every string's characters, so the reference for "" reads 0 instead of failing.
Example 2 — What fixed Does with It (Unsafe Code)
Pin a string and read characters through a pointer. This needs AllowUnsafeBlocks in the project.
C#
using System;
class Program {
static unsafe void Main() {
string text = "C#!";
fixed (char* p = text) {
Console.WriteLine(p[0]);
Console.WriteLine(p[2]);
Console.WriteLine((int)p[text.Length]);
}
}
}
Output
C
!
0
How It Works
Unsafe on purpose.unsafe on Main allows pointers. The project must also set AllowUnsafeBlocks, or the compiler refuses.
Pinning.fixed (char* p = text) stops the garbage collector from moving the string, and p points at its first character. Pinnable types expose GetPinnableReference() for exactly this pattern.
Reading through the pointer.p[0] and p[2] read C and !, just like text[0] and text[2].
The terminator.p[text.Length] is the hidden '\0' after the text. Only read; writing through p would break immutability for every user of that string.
Example 3 — The Everyday Alternative: AsSpan()
Get the same no-copy access to the characters with a ReadOnlySpan<char>, in fully safe code.
C#
using System;
class Program {
static void Main() {
string text = "Hello World";
ReadOnlySpan<char> span = text.AsSpan();
Console.WriteLine(span[0]);
Console.WriteLine(span.Length);
Console.WriteLine(span.Slice(6).ToString());
}
}
Output
H
11
World
How It Works
No pointers needed.AsSpan() gives a read-only view over the string's characters without copying them.
Index and length.span[0] and span.Length work like they do on the string.
Cheap slicing.Slice(6) is a smaller view starting at index 6; only the final ToString() creates a new string, World.
Choose this first. Spans are safe, need no project settings, and cover nearly every case where people reach for GetPinnableReference.
Frequently Asked Questions
It returns a read-only reference to the first character of a string. It exists so a string fits the pattern used by the fixed statement to pin objects in memory.
Almost never. It is intended to support compilers and low-level code. Use text[i], foreach, or text.AsSpan() instead.
In unsafe code, write fixed (char* p = text) { ... }. The project must enable AllowUnsafeBlocks. Only read through the pointer, never write.
A reference to the null terminator character ('\0') that .NET stores after the characters of every string.
.NET Core 3.0 and later, including .NET 5+. It is not on .NET Framework, where fixed on a string still works through built-in compiler support.