TL;DR

Microsoft is developing a significant update to C# memory safety, redesigning the unsafe keyword to make safety contracts explicit. The new model will be previewed in .NET 11 and released in .NET 12, aiming to enhance code security and reviewability.

Microsoft is actively developing a new memory safety model for C# that will overhaul the way unsafe code is handled, making safety contracts more explicit and enforceable by the compiler. This update aims to improve security and code review processes in C# applications.

The new model redefines the unsafe keyword in C# to serve as a safety contract rather than just a syntax marker. It expands from marking pointers to encompassing any code interacting with memory in ways the compiler cannot verify as safe. This change will be introduced as a preview in .NET 11 and become a production feature in .NET 12, initially opt-in, with potential to become the default. The early implementation has already landed in the main branch of the compiler. Historically, C#’s unsafe context has allowed developers to access pointers and unmanaged memory, which can lead to undefined behavior if misused. The redesigned unsafe keyword will enforce stricter review and clearer boundaries, aligning more closely with models used in Rust and Swift. This effort reflects industry-wide emphasis on memory safety, especially as AI-assisted code generation increases the scale of software production, raising security concerns. The new model aims to make unsafe code more transparent, reviewable, and safer, reducing risks of memory-related vulnerabilities.

Why It Matters

This development is significant because it enhances C#’s ability to prevent memory safety issues, which are a primary source of security vulnerabilities and bugs in software. By making safety contracts explicit and enforceable, it improves code quality, security, and maintainability. It also aligns C# with other languages like Rust and Swift that have stricter safety semantics. As software systems become more complex and automated code generation grows, these improvements are vital for industry standards and security compliance.

Amazon

C# memory safety development kit

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Background

Since its introduction in C# 1.0, the unsafe keyword has been used to allow developers to work with pointers and unmanaged memory, primarily for interoperability and performance. Learn more about memory management. Over time, other languages like Rust and Swift adopted stricter, propagation-oriented semantics for unsafe code, emphasizing safety contracts. Microsoft’s ongoing efforts aim to modernize C#’s approach, aligning it with these models. The upcoming changes are part of a broader initiative to improve safety in the .NET ecosystem, especially as industry and government prioritize memory safety and security. The early compiler modifications have already been integrated, indicating active progress toward these goals.

“The new safety model will make safety contracts in unsafe code explicit and enforceable, improving security and reviewability.”

— Microsoft developer team

“Aligning C# unsafe semantics with Rust’s propagation model will help developers better understand and manage unsafe operations.”

— C# language architect

Elevator Debugging Tools TCM Manager Copy Program Modify Parameters

Elevator Debugging Tools TCM Manager Copy Program Modify Parameters

Elevator Debugging Tools TCM Manager Copy Program Modify Parameters

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What Remains Unclear

It is not yet clear how widely adopted the new model will be in early releases or whether it will become the default in future versions. Details about tooling updates and developer migration strategies are still emerging.

Amazon

C# unsafe 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 the new safety model as a preview in .NET 11, with full support in .NET 12. Developers and teams are encouraged to experiment with the preview and provide feedback. Further updates on tooling, documentation, and best practices are expected ahead of the full release.

60" Trampoline for Kids, 5 FT Toddler Baby Trampoline with Safety Enclosure Net, Indoor or Outdoor Blue Small Trampolines with Basketball Hoop, Birthday Gifts for Kids, Gifts for Boy Age 3-8

60" Trampoline for Kids, 5 FT Toddler Baby Trampoline with Safety Enclosure Net, Indoor or Outdoor Blue Small Trampolines with Basketball Hoop, Birthday Gifts for Kids, Gifts for Boy Age 3-8

STURDY – Rust-proof steel structure with a total of 3 U-shape legs for added stability impressive bouncing. Each…

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Key Questions

What is changing about the unsafe keyword in C#?

The unsafe keyword will be redesigned to serve as a safety contract, making the obligations and boundaries of unsafe code explicit and enforceable by the compiler, rather than just a syntax marker.

When will the new memory safety model be available?

The preview will be available in .NET 11, with the full release planned for .NET 12.

Will this change affect existing unsafe code?

Existing unsafe code will continue to work, but developers may need to update code to adhere to the new safety contracts and review guidelines introduced in the new model.

Why is this update important for software security?

By making safety boundaries explicit and enforceable, the update reduces the risk of memory-related vulnerabilities, which are a common source of security bugs and exploits.

Source: Hacker News

You May Also Like

Project Glasswing: An Initial Update

Initial findings from Project Glasswing reveal AI models discovering thousands of vulnerabilities in critical software, accelerating cybersecurity efforts.

BambuStudio has been violating PrusaSlicer AGPL license since their fork

BambuStudio has been found to violate the AGPL license of PrusaSlicer by using a closed-source networking plugin, raising legal and ethical concerns.

The memory shortage is causing a repricing of consumer electronics

Global memory supply constraints are driving up costs, causing a decline in affordable smartphones and impacting consumer electronics worldwide.

SpaceX S-1

SpaceX has filed for an initial public offering, seeking to list on Nasdaq under the symbol ‘SPCX.’ The move marks a major shift for the private aerospace company.