Improving C# Memory Safety

TL;DR

Microsoft is developing a new safety model for C# that redefines the unsafe keyword to improve memory safety. The update, expected in .NET 11 and 12, aims to make unsafe code more transparent and enforceable, reducing security risks.

Microsoft is redesigning the unsafe keyword in C# to improve memory safety by making safety contracts explicit and enforceable, with a preview expected in .NET 11 and a full release in .NET 12.

The new model expands the use of the unsafe keyword beyond marking pointers, turning it into a tool for defining safety contracts that the compiler can enforce. Currently, unsafe code in C# allows developers to perform memory operations that the compiler cannot verify as safe, posing security and stability risks. The upcoming changes aim to make these unsafe operations more transparent and reviewable by introducing a safety comment style and stricter compiler enforcement.

This redesign aligns C# more closely with languages like Rust and Swift, which treat unsafe code as explicit contracts rather than syntax markers. The new model will initially be opt-in, with the possibility of becoming the default in future releases. The early implementation has already landed in the main branch of the compiler, signaling active development.

Why It Matters

This update is significant because it addresses growing concerns over memory safety in software development, especially as AI-assisted code generation accelerates the creation of complex applications. By making unsafe code more explicit and enforceable, Microsoft aims to reduce security vulnerabilities, improve code review processes, and uphold industry standards for safe software engineering.

Zig Language for Low-Level Engineering: C Replacement with Memory Safety, Cross-Compilation, and Embedded Support

Zig Language for Low-Level Engineering: C Replacement with Memory Safety, Cross-Compilation, and Embedded Support

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Background

Memory safety has been a key focus in modern programming languages, with C# introducing the unsafe keyword in version 1.0 to allow pointer operations. Over time, other languages like Rust and Swift have adopted stricter, propagation-oriented semantics for unsafe code. The industry’s increasing reliance on native interop and performance-critical code has heightened the need for clearer safety boundaries. Microsoft’s upcoming changes reflect ongoing efforts to enhance .NET’s safety mechanisms, building on existing runtime and language features such as bounds checking and null safety.

“The redesigned unsafe keyword will make safety contracts explicit, reviewable, and enforceable, improving overall memory safety in C#.”

— Microsoft spokesperson

“Aligning C# unsafe semantics with Rust’s model allows for better safety guarantees and clearer developer responsibilities.”

— C# language team member

Free Fling File Transfer Software for Windows [PC Download]

Free Fling File Transfer Software for Windows [PC Download]

Intuitive interface of a conventional FTP client

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What Remains Unclear

Details remain unclear on the exact syntax of the new safety comments, how the enforcement will handle complex unsafe scenarios, and whether the new model will be enabled by default in future releases. Additionally, the timeline for full adoption and the impact on existing codebases are still developing.

BearTOOLS USA Flag Safety Tool Lanyard – Tough Scaffold Tether with – Stop The Drops - Ultra-Durable and Sturdy – Heavy-Duty 8kg Max Load – Construction Sites, American Design, 36 inches (10PK)

BearTOOLS USA Flag Safety Tool Lanyard – Tough Scaffold Tether with – Stop The Drops – Ultra-Durable and Sturdy – Heavy-Duty 8kg Max Load – Construction Sites, American Design, 36 inches (10PK)

AMERICAN FLAG DESIGN: Looking for a practical and effective way to increase safety on the construction site? Represent…

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What’s Next

Microsoft plans to release a preview of the new safety model in .NET 11, with feedback from the developer community. The final, production-ready version is expected in .NET 12. Developers should monitor updates and consider testing the preview to prepare for upcoming changes.

Amazon

memory safety testing tools for C#

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Key Questions

What is the main goal of the new C# unsafe safety model?

The main goal is to make memory safety contracts explicit, reviewable, and enforceable to reduce security risks and improve code clarity.

Will the new safety features be enabled by default?

Initially, the new model will be opt-in, with the possibility of becoming the default in future releases.

How does this compare to unsafe handling in Rust or Swift?

The new C# model aims to treat unsafe code as explicit safety contracts, similar to Rust’s propagation-oriented semantics, rather than just syntax markers.

What should developers do now?

Developers should follow updates from Microsoft, experiment with the preview in .NET 11, and review their unsafe code practices in anticipation of the new safety model.

Source: Hacker News

You May Also Like

Valorant’s new Vanguard update seems to be bricking cheaters’ PCs. Riot’s response? “Congrats on your $6k paperweights”

Riot Games states Vanguard anti-cheat does not ‘brick’ PCs, clarifying recent claims about hardware damage linked to a recent update.

OpenBSD 7.9 Released

OpenBSD 7.9, the latest stable release, launched on May 19, 2026, introducing new hardware support, kernel improvements, and virtualization updates.

Unlocking asynchronicity in continuous batching

A new approach separates CPU and GPU workloads in continuous batching, reducing idle time and boosting inference performance.

Webcam and Microphone Placement for Better Calls (No Fancy Gear Needed)

Webcam and microphone placement tips for better calls without fancy gear—discover simple tricks to enhance your video and audio quality easily.