C# 16 preview (2026): memory safety and what's next
What you’ll learn
Section titled “What you’ll learn”- 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.
Summary
Section titled “Summary”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.
Release at a glance
Section titled “Release at a glance”| 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 map
Section titled “Feature map”| 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 |
How to run the samples
Section titled “How to run the samples”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”Problem
Section titled “Problem”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.
How it works
Section titled “How it works”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.
Example
Section titled “Example”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 1The 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”.
Gotchas
Section titled “Gotchas”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.
When not to use it
Section titled “When not to use it”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”Problem
Section titled “Problem”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.
How it works
Section titled “How it works”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.
Example
Section titled “Example”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: rejectedlength 1: rejectedlength 2: 0xCAFElength 4: 0xCAFEThe 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.
Gotchas
Section titled “Gotchas”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.
When not to use it
Section titled “When not to use it”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.
Breaking changes labeled C# 16
Section titled “Breaking changes labeled C# 16”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 => { })withM(F1)andM(F2)overloads, whereF1takesint*, printed2under C# 15 and fails with CS0121 underpreviewin testing. Give the lambda explicit parameter types. safeis a contextual keyword (18.9). A type namedsafeused as a return type no longer parses;safe M1() => new safe();failed with “CS1520: Method must have a return type”. Escape it as@safe.unsafeis required for more members (18.9). For example, using anunsafeindexer 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.
In progress, not committed
Section titled “In progress, not committed”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 awaitseonly when it isn’t null. - Chained relational comparison:
min <= x <= maxmeaningmin <= 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
partialanywhere 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:
constdeclarations insideextensionblocks. - Type Parameter Inference from Constraints: let generic constraints take part in type inference, so fewer calls need explicit type arguments.
Upgrade steps and gotchas
Section titled “Upgrade steps and gotchas”- Fix the mechanical breaks: types or members named
safe, ambiguous lambdas passed to overloads that take pointer delegates, andunsafemodifiers on types. - Push
unsafedown to the access. Shrink largeunsafeblocks 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.
Interview questions
Section titled “Interview questions”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.
Key takeaways
Section titled “Key takeaways”- 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
LangVersionpreviewplus theupdated-memory-safety-rulesfeature;AllowUnsafeBlocksstays separate. - Pointer declarations become safe; memory access stays unsafe;
unsafemembers 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.
Related pages
Section titled “Related pages”- C# versions: release history and guides
- C# 15 (2026)
- unsafe and fixed
- Span and ref structs
- Memory and performance hub
Official release notes
Section titled “Official release notes”There are no C# 16 release notes yet. These are the versioned official sources for the preview:
- What’s new in C# 15: memory safety (preview)
- Enable the updated memory safety rules (compiler options)
- Unsafe code: the updated memory safety model (preview)
- The
safekeyword - Feature specification: unsafe evolution
- Compiler breaking changes from .NET 10.0.100 to .NET 11.0.100 (includes the C# 16 entries)
- Roslyn Language Feature Status (working set)
- C# language proposals (dotnet/csharplang)
- Releases and support for .NET