Improving C# Memory Safety
AIThis post was created with the assistance of artificial intelligence (AI).

TL;DR

Before you orderOffer from Amazon

Get the latest gadgets delivered free with Prime

  • Fast, free delivery on millions of items
  • Prime Video, Amazon Music and more included
  • Member-only deals all year
Start your free Prime trial Free trial for eligible customers · Cancel anytime
As an affiliate, we earn on qualifying purchases.

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.

Amazon

C# code review tools

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

unsafe code debugging tools

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

FALL

Fall Picks

As an affiliate, we earn on qualifying purchases.

You May Also Like

Apple’s iPhone 18 Pro Max might come with a massive battery

Rumors suggest the iPhone 18 Pro Max could include a significantly larger battery, potentially extending battery life. Details remain unconfirmed.

Europe Regulated the Interface and Forgot to Build the Engine

Brussels is moving to simplify cookie consent while InvestAI aims to build AI capacity, exposing Europe’s gap with US and Chinese labs.

Kiki – a tiny homepage construction kit with a small footprint

Kiki is a lightweight, PHP-based homepage builder designed for simplicity, with a small codebase and no dependencies. Available as shareware on itch.io.

Vivaldi 8.0

Vivaldi 8.0 introduces a unified interface, six preset layouts, and enhanced customization, marking its biggest update in years.