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.

Amazon

C# unsafe code safety tools

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

Amazon

memory safety analysis tools for C#

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.

FOXWELL NT301 OBD2 Scanner Live Data Professional Mechanic OBDII Diagnostic Code Reader Tool for Check Engine Light

FOXWELL NT301 OBD2 Scanner Live Data Professional Mechanic OBDII Diagnostic Code Reader Tool for Check Engine Light

【Vehicle CEL Doctor】The NT301 obd2 scanner enables you to read DTCs, access to e-missions readiness status, turn off…

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.

Code Hard Debug Harder Debugging Programmer Case for iPhone 17

Code Hard Debug Harder Debugging Programmer Case for iPhone 17

Coding themed programmer design for software developers and all programming lovers.

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

Gooey: A GPU-accelerated UI framework for Zig

Gooey introduces a GPU-accelerated, declarative UI framework for Zig, supporting macOS, Linux, and WebAssembly, with ongoing development and new features.

Why is Google Maps back to showing old satellite images of Altadena?

Google Maps is showing outdated satellite imagery of Altadena, raising questions about the cause and impact of this change.

Finland’s last analogue landline phones go silent after 150 years

Finland has shut down its analogue landline phones, marking the end of an era after nearly 150 years of service, as the country fully transitions to digital networks.

OpenAI feels “burned” by Apple’s crappy ChatGPT integration, insiders say

OpenAI is reportedly exploring legal options after Apple’s subpar ChatGPT integration failed to meet expectations, causing frustration and stalled negotiations.