AIThis post was created with the assistance of artificial intelligence (AI).

TL;DR

Buying for a business?Offer from Amazon

Get business pricing on monitors, keyboards and dev gear

  • Business-only prices and quantity discounts
  • Tax-exempt purchasing
  • Multiple users, one account, clear invoices
As an affiliate, we earn on qualifying purchases.

Microsoft is redesigning the unsafe keyword in C# to make memory safety contracts clearer and enforceable. The new model, expected in .NET 11 and 12, aims to improve security and code review for unsafe operations.

Microsoft is implementing a significant update to C# memory safety by redesigning the unsafe keyword to explicitly communicate safety obligations, starting with a preview in .NET 11 and a full release in .NET 12.

The new model expands the scope of the unsafe keyword from marking pointers to encompassing any code that interacts with memory in ways the compiler cannot verify as safe. The unsafe keyword will now serve as a contract, with the compiler enforcing that unsafe operations are properly encapsulated and reviewed. This change aims to make safety assumptions more transparent and reviewable, reducing risks associated with unsafe code. The early implementation has already landed in the main branch of the C# compiler, with plans to release the feature as an opt-in in the upcoming .NET 11 preview, becoming the default in .NET 12. The redesign aligns C# more closely with Rust’s approach, emphasizing explicit safety contracts over implicit conventions. Most developers will not notice immediate changes unless they actively use unsafe APIs, but the broader surface area of unsafe code will be subject to stricter review and enforcement.

Why It Matters

This update is important because it enhances memory safety in C# programs, which is critical given the rising importance of security and the proliferation of AI-assisted code generation. By making safety contracts explicit, the new model reduces the likelihood of undefined behavior and security vulnerabilities caused by unsafe memory access. It also supports industry and government standards for safer software development, especially in contexts where memory safety is paramount.

Amazon

C# unsafe code review tools

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Background

C# introduced the unsafe keyword in version 1.0 to allow pointer operations within specific contexts. Over time, unsafe has been used for interop and performance-critical code, but its semantics have been loosely defined. Other languages like Rust and Swift have adopted stricter, propagation-oriented semantics for their unsafe features. Microsoft’s new approach in C# aims to unify and clarify this model, making unsafe code more transparent and reviewable. The upcoming changes follow ongoing efforts to improve .NET’s structural safety mechanisms, including bounds checking and null safety, to create a more secure development environment.

“The unsafe keyword will now serve as a safety contract, with the compiler enforcing proper encapsulation and review, making safety assumptions explicit.”

— Microsoft C# team

“The new safety model will initially be opt-in, with plans to make it the default in future releases, aligning C# with modern safety practices.”

— Microsoft spokesperson

Amazon

memory safety debugging tools for C#

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 adoption will be in the initial phases, or how existing codebases will transition to the new model. Details on tooling support and developer workflows are still emerging.

Amazon

C# unsafe keyword reference book

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What’s Next

Microsoft will release a preview of the new safety model in .NET 11, allowing developers to test and provide feedback. The feature is expected to become the default in .NET 12, with updates to development templates and tooling support. Further details on migration strategies and best practices are anticipated in upcoming developer documentation and community discussions.

Amazon

C# memory safety development kit

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Key Questions

What is the main change to the unsafe keyword in C#?

The unsafe keyword will now act as a safety contract, with the compiler enforcing that unsafe operations are properly encapsulated and reviewable, rather than just marking pointer code.

When will the new memory safety model be available?

A preview will be released in .NET 11, with full adoption expected in .NET 12 as the default safety model.

Will existing unsafe code need to be rewritten?

It is not yet clear how migration will be handled, but initial plans suggest that existing code can continue to work, with optional updates to adopt the new safety contracts.

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

The new C# model aligns more with Rust’s approach, emphasizing explicit safety contracts and reviewability, whereas earlier C# unsafe code was more implicit.

Source: Hacker News

HALLOWEEN

Halloween Picks

As an affiliate, we earn on qualifying purchases.

You May Also Like

The Safety Card, Played From Every Side: David Sacks, Anthropic, and the Fable Standoff

White House official alleges Anthropic refused to fix a cyberweapon jailbreak, leading to model bans; Anthropic disputes the severity of the issue.

Could AI Have Been The Silent Force Behind The Coldcard Hack Detection?

Exploring the potential influence of AI on the Coldcard hardware wallet breach and the implications for security review processes.

The bank account in the chat. How personal finance became an agentic on-ramp.

OpenAI introduces bank account integration in ChatGPT for Pro users, marking a shift toward agentic consumer finance and changing fintech dynamics.

Microsoft degrades functionality of perpetually-licensed offline products

Microsoft will disable full functionality of Office 2019 for Mac after July 13, 2026, reducing it to view-only mode due to license certificate expiration.