Skip to content

C# 16 preview (2026): memory safety and what's next

  • What Microsoft has published about the next C# version as of November 15, 2026, and what it hasn’t.
  • Why Microsoft documents the memory safety rules as a “C# 15 preview” in one place and as “C# 16” in another.
  • How the preview changes the meaning of unsafe, and how to turn it on.
  • How to write a requires-unsafe API and the safe boundary in front of it.
  • Which proposals are in the compiler team’s working set, and why you shouldn’t plan around them yet.

The next C# version is in early preview: its only usable piece is the memory safety redesign in the .NET 11 SDK, opt-in through LangVersion preview.

Item Value
Released Not released. No date announced.
Paired runtime Not announced. The preview runs on .NET 11 (11.0.0). .NET ships a major release every November, so .NET 12 would be expected in November 2027 if that cadence holds.
Minimum toolset .NET 11 SDK (11.0.100). Visual Studio: not stated by Microsoft for the preview as a whole; the breaking-change list dates individual changes to Visual Studio 2026 versions 18.7 and 18.9.
Default for TFM None. No target framework selects it by default.
LangVersion preview, plus the updated-memory-safety-rules compiler feature for the caller rules. The breaking-change list writes langversion:16, which the .NET 11 SDK doesn’t accept as a released value.
Support today Preview only. Not supported for production.
Previews in this release Memory safety (pointer relaxations, unsafe(...) expressions, requires-unsafe members, the safe keyword).

Does Microsoft call this C# 16? Partly. The language reference, the compiler options page and What’s new in C# 15 all describe the updated memory safety rules as “a preview feature in C# 15 and .NET 11”. Only the .NET 11 compiler breaking-change list describes the same changes as “In C# 16” or “under langversion:16”. The likely reading is that the preview ships in the C# 15 toolset and is expected to become stable in a later C# version, which the breaking-change list labels C# 16. Microsoft hasn’t published a “What’s new in C# 16” page, and its “What’s new” pages add features only “when they’re available in public preview releases”.

Feature Weight Mainly for Deep dive
Pointer relaxations and unsafe(...) expressions Headline (preview) Interop and parsing libraries unsafe and fixed
Requires-unsafe members and the safe keyword Headline (preview) Library authors, security review unsafe and fixed
Ten “in progress” proposals Not shipped Planning only Roslyn Language Feature Status

The samples need the .NET 11 SDK. Create a console project with dotnet new console and add:

<PropertyGroup>
<LangVersion>preview</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Features>$(Features);updated-memory-safety-rules</Features>
</PropertyGroup>

preview enables the new syntax and the pointer relaxations. The updated-memory-safety-rules feature enables the caller rules, and the compiler records the choice in the assembly with MemorySafetyRulesAttribute. AllowUnsafeBlocks is independent: it still controls whether the source may use the unsafe keyword at all. Microsoft plans a stable MemorySafetyRules project property for when the feature leaves preview, but it isn’t implemented yet.

Both samples were compiled with the C# compiler from the .NET 11.0.100 SDK (compiler 5.12.0) with implicit usings, -langversion:preview -unsafe, with and without the feature flag, and run on .NET 11.0.0. The output is shown under each sample.

Headline feature (preview): pointer relaxations and unsafe expressions

Section titled “Headline feature (preview): pointer relaxations and unsafe expressions”

In C# 15 and earlier, unsafe is tied to the existence of pointer types. Declaring an int*, taking an address, pinning with fixed or using sizeof on your own struct all need an unsafe context, even though none of them can corrupt memory. The dangerous operations are the ones that access memory through a pointer. Because unsafe blocks end up large and cover harmless code too, a reviewer can’t easily find the lines that need an audit.

With LangVersion preview, declaring a pointer type, taking an address with &, the fixed statement, converting stackalloc to a pointer and sizeof on any unmanaged type no longer need an unsafe context. Dereferencing (*p), member access (p->x), element access (p[i]) and calling a function pointer still do. The new unsafe(expression) form opens an unsafe context for one expression, which also works where a block can’t appear, such as a field initializer, a constructor initializer or a catch filter.

An OrdersPortal service reads the 4-byte header that a label printer sends back: a big-endian magic number, a protocol version and a flags byte.

using System.Globalization;
using System.Runtime.InteropServices;
// An OrdersPortal label printer answers a status request with a 4-byte header:
// a big-endian magic number, a protocol version and a flags byte, then the payload.
byte[] reply = [0xCA, 0xFE, 0x02, 0x01, 0x4F, 0x4B];
int headerSize = sizeof(LabelHeader); // sizeof on a struct: no unsafe context
fixed (byte* start = reply) // pinning: no unsafe context
{
LabelHeader* header = (LabelHeader*)start; // declaring and casting a pointer: no unsafe context
// Reading through the pointer accesses memory, so it still needs an unsafe context.
// unsafe(...) scopes that to one expression.
ushort magic = unsafe((ushort)((header->MagicHigh << 8) | header->MagicLow));
unsafe
{
Console.WriteLine(string.Create(CultureInfo.InvariantCulture,
$"header {headerSize} bytes, magic 0x{magic:X4}, version {header->Version}, flags {header->Flags}"));
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct LabelHeader
{
public byte MagicHigh;
public byte MagicLow;
public byte Version;
public byte Flags;
}

Output:

header 4 bytes, magic 0xCAFE, version 2, flags 1

The same file compiled with LangVersion 15 fails with “CS0233: ‘LabelHeader’ does not have a predefined size, therefore sizeof can only be used in an unsafe context” and “CS0214: Pointers and fixed size buffers may only be used in an unsafe context”.

Relaxing the declaration rules doesn’t make a pointer valid: header still points into a pinned array, and using it after the fixed block ends is a use-after-unpin bug that the compiler won’t catch. The cast to LabelHeader* is only correct because the struct’s layout is sequential, all four fields are single bytes (so there is no padding to worry about), and the reply is at least four bytes long; the sample’s fixed data guarantees the length, real input doesn’t. unsafe(...) covers the whole expression inside the parentheses, so keep it to the access itself.

If Span<T>, MemoryMarshal.Read<T> or BinaryPrimitives.ReadUInt16BigEndian can do the job, use them: they are bounds-checked and need no unsafe code at all. The relaxations help code that genuinely needs pointers, such as interop and hand-tuned parsers.

Headline feature (preview): requires-unsafe members

Section titled “Headline feature (preview): requires-unsafe members”

Under the original rules, unsafe on a method only permits pointers inside it. It tells callers nothing, so a method that silently assumes “the buffer has at least two bytes” looks as safe to call as any other.

With the updated-memory-safety-rules flag, unsafe on a member marks it requires-unsafe: every caller must be in an unsafe context, which moves the audit obligation to the call site. The modifier no longer opens an unsafe context for the member’s own body, so the body needs an explicit unsafe block around its memory access. Microsoft also recommends a <safety> XML documentation tag that states what the caller must guarantee; the compiler copies it to the documentation file but doesn’t check it. The pattern that follows is a requires-unsafe primitive plus a safe method that checks the contract and discharges the obligation.

using System.Globalization;
byte[][] packets = [[], [0xCA], [0xCA, 0xFE], [0xCA, 0xFE, 0x00, 0x01]];
foreach (byte[] packet in packets)
{
// The safe overload validates, so callers need no unsafe context.
string result = NativeBuffer.TryReadUInt16(packet, 0, out ushort magic)
? string.Create(CultureInfo.InvariantCulture, $"0x{magic:X4}")
: "rejected";
Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $"length {packet.Length}: {result}"));
}
static class NativeBuffer
{
/// <summary>Reads a big-endian UInt16.</summary>
/// <safety>source must point to at least two readable bytes.</safety>
public static unsafe ushort ReadUInt16(byte* source)
{
unsafe
{
return (ushort)((source[0] << 8) | source[1]);
}
}
// A safe boundary: it checks the contract, then discharges the obligation in one unsafe expression.
public static bool TryReadUInt16(byte[] buffer, int offset, out ushort value)
{
if (offset < 0 || buffer.Length - offset < 2)
{
value = 0;
return false;
}
fixed (byte* start = buffer)
{
value = unsafe(ReadUInt16(start + offset));
}
return true;
}
}

Output:

length 0: rejected
length 1: rejected
length 2: 0xCAFE
length 4: 0xCAFE

The length check is the whole point. Written as buffer.Length - offset < 2, it can’t overflow and it rejects empty and one-byte buffers before fixed runs (for an empty array, fixed yields a null pointer). A tempting alternative, (uint)offset > (uint)(buffer.Length - 2), fails for a one-byte buffer because 1 - 2 is -1, which becomes uint.MaxValue and lets offset 0 through to an out-of-bounds read. Checking exactly this kind of arithmetic is the obligation that requires-unsafe pushes onto whoever writes unsafe(...) at the call site.

Remove unsafe(...) from the call and, with the flag on, the build fails with “CS9362: ‘NativeBuffer.ReadUInt16(byte*)’ must be used in an unsafe context because it is marked as ‘unsafe’”. Without the flag, it fails with CS9363 instead (“because it has pointers in its signature”): pointer signatures still require unsafe callers, but only the flag makes the unsafe modifier itself carry that meaning, including for members whose signatures have no pointers.

Rules across assemblies depend on which side opts in. Both updated: the unsafe markers travel through metadata. Updated caller, original-model library: a compatibility mode treats any member with a pointer in its signature as requires-unsafe. Original-model caller, updated library: the old rules apply, so a requires-unsafe member without pointers in its signature becomes callable from safe code. The model also adds a safe contextual keyword for declarations that carry a safety contract, such as extern members and fields in explicit or extended layout types.

Don’t mark a member requires-unsafe if it validates its own inputs; that just spreads unsafe to callers who have nothing to check. And don’t enable the flag in a library you publish today: consumers on the original model won’t see the markers, and the preview semantics can change between SDK updates.

The .NET 11 compiler breaking-change list puts these under C# 16 or langversion:16; in the .NET 11 SDK they appear with LangVersion preview:

  • Pointer types no longer require an unsafe context (Visual Studio 2026 version 18.7). Lambdas can now convert to delegates with pointer parameters, so overload resolution can become ambiguous: a call M(x => { }) with M(F1) and M(F2) overloads, where F1 takes int*, printed 2 under C# 15 and fails with CS0121 under preview in testing. Give the lambda explicit parameter types.
  • safe is a contextual keyword (18.9). A type named safe used as a return type no longer parses; safe M1() => new safe(); failed with “CS1520: Method must have a return type”. Escape it as @safe.
  • unsafe is required for more members (18.9). For example, using an unsafe indexer with a pointer parameter now reports CS9363 outside an unsafe context.

The unsafe evolution specification adds more for code that enables the updated rules: extern members and fields in explicit-layout types need an explicit unsafe or safe, some stackalloc uses need an unsafe context, an override can’t be unsafe when the base member is safe, and unsafe on a type declaration, static constructor or finalizer becomes an error because it has no effect.

The Roslyn Language Feature Status page lists these C# items in the in-progress part of its working set (the labels are Roslyn’s). Most have a csharplang proposal and a feature branch or pull request, but none has a shipped preview or a target version. The one-line meanings come from the proposal summaries in the dotnet/csharplang repository; this page shows no syntax beyond that, because nothing is documented on Microsoft Learn yet.

  • Dictionary expressions: collection-expression syntax for dictionaries, such as ["mads": 21, "dustin": 22].
  • Null-conditional await: await? e, which awaits e only when it isn’t null.
  • Chained relational comparison: min <= x <= max meaning min <= x && x <= max.
  • Target-typed static member access: omit the type name when it matches the target type, as with enum values or factory methods.
  • Relax modifier ordering: allow partial anywhere in a modifier list.
  • Compound assignment in initializers: += and similar inside object initializers, for example to attach event handlers.
  • Extension members on typeless receivers: call extension members on expressions with no natural type, such as a collection expression.
  • Runtime Async Streams: extend runtime async, in which the .NET 11 runtime rather than a compiler-generated state machine suspends and resumes async methods, to async iterators.
  • Extension constants: const declarations inside extension blocks.
  • Type Parameter Inference from Constraints: let generic constraints take part in type inference, so fewer calls need explicit type arguments.
  1. Fix the mechanical breaks: types or members named safe, ambiguous lambdas passed to overloads that take pointer delegates, and unsafe modifiers on types.
  2. Push unsafe down to the access. Shrink large unsafe blocks so each covers only dereferences and element access, and put a validating safe method in front of each requires-unsafe primitive.

Where it sits in the language’s evolution

Section titled “Where it sits in the language’s evolution”

Since C# 1.0 added pointers, the language has mostly reduced the need for them, with Span<T> and ref struct (C# 7.2), ref fields and scoped (C# 11) and inline arrays (C# 12). The memory safety work is the first time it redefines what unsafe itself means, so that the remaining unsafe code is small, explicit and easy to audit.

Under the preview rules, which pointer operations still need an unsafe context? Operations that access memory through a pointer: dereference (*p), member access (p->x), element access (p[i]) and calling a function pointer. Declaring pointer types, taking addresses, fixed, stackalloc to a pointer and sizeof don’t.

What does “requires-unsafe” mean, and who checks the contract? With the updated rules, unsafe on a member means every caller must be in an unsafe context. The compiler only enforces that the caller acknowledged the obligation; the caller’s code, and its reviewer, must actually check the contract, for example a length check before reading two bytes.

Why is LangVersion preview alone not enough to get the new caller rules? preview enables the syntax and pointer relaxations. The caller obligations are a separate compiler feature, updated-memory-safety-rules, so projects can adopt the syntax before taking on new errors.

Senior scenario: a team wants to enable the preview across a 200-project solution to “get ahead” of C# 16. What do you advise? Don’t. Pick one or two projects that own the interop and parsing code and try it on a branch with preview and the flag turned on together, so you see both kinds of errors; then measure the breaks and the review benefit. Keep shipping projects on C# 15, because LangVersion preview can change with every SDK update, and don’t publish libraries built with the flag, because original-model consumers can call requires-unsafe members from safe code without noticing. Revisit when Microsoft publishes a “What’s new” page for the release and the MemorySafetyRules property.

  • The memory safety redesign is the only usable piece of the next C# version; Learn calls it a C# 15 preview, and the breaking-change list labels its changes C# 16.
  • Enable it with LangVersion preview plus the updated-memory-safety-rules feature; AllowUnsafeBlocks stays separate.
  • Pointer declarations become safe; memory access stays unsafe; unsafe members push the audit to callers.
  • Put a validating safe boundary in front of every requires-unsafe primitive.
  • Everything else is “in progress” on the Roslyn status page and may change or be dropped.

There are no C# 16 release notes yet. These are the versioned official sources for the preview: