What's new in C# 15: Two features for library authors
The last two C# 15 features matter most to people who write libraries and low-level code. Here's what they are, in plain words, so you know them when you meet them.
By Gregor Dzierzon ·

What's new in C# 15
- The release: .NET 11, LTS and STS
- Union types
- Labeled
breakandcontinue- Closed hierarchies
- Collection expression arguments
- Extension indexers
- For library authors: static interface members on older runtimes, and memory safety
For library authors
The last two features in C# 15 aren't ones most of us will type every day. They matter to people who write libraries, code that other programs download and use, and to people who write low-level code that works directly with memory. You'll still meet them in other people's code, so here's what each one is for.
Static interface members on older runtimes

An interface is a promise: "anything that implements me has these members." Sometimes a helper method belongs with that promise, so you put a static method on the interface itself and call it through the interface's name:
public interface IMeasurement
{
public static double ToMeters(double centimeters) => centimeters / 100;
}
double meters = IMeasurement.ToMeters(250); // 2.5
The problem: many libraries are built for older targets too, such as .NET Standard 2.0, so that apps on the older .NET Framework can still use them. Until now, the compiler refused a helper like this when building for those targets. Build this interface for .NET Standard 2.0 with C# 14, and it fails with error CS9327, "Feature 'static members in interfaces' is not available in C# 14.0."
The C# 15 answer: a plain (non-virtual) static member on an interface now works even when the target runtime doesn't support default interface implementations. The same code builds for .NET Standard 2.0 with C# 15. The helper can live where it belongs, on the interface, without leaving older apps behind.
It's a narrow change. Other interface features that need help from the runtime, such as default implementations of instance methods and static abstract members, still need a newer runtime.
Memory safety (a preview)
Most C# code never touches memory directly: .NET manages it for you. But for speed, or to talk to code written in other languages, C# lets you use pointers, variables that hold a memory address. Pointers are powerful and easy to get wrong, so C# makes you mark that code with the word unsafe, as a warning sign to every reader.
The problem: today, the sign goes up for anything that mentions a pointer, even lines that can't hurt anything. Declaring a pointer, or taking the address of a variable, needs unsafe just as much as reading the memory it points to does. When everything is marked dangerous, the sign stops meaning much.
The C# 15 preview: it starts to tie unsafe to the operations that actually access memory, not to the mere mention of a pointer:
int number = 42;
int* pointer = &number; // no longer needs unsafe: nothing is read or written yet
unsafe
{
Console.WriteLine(*pointer); // reading through the pointer still does
}

The preview also makes unsafe on a method a contract: whoever calls that method has to be in an unsafe block too, so the warning sign travels with the danger instead of hiding inside the method.
This is a preview, so it's off unless you switch it on. In the project file:
<PropertyGroup>
<LangVersion>preview</LangVersion>
<Features>$(Features);updated-memory-safety-rules</Features>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Microsoft calls it the start of "a multirelease effort to redefine memory safety in the language", so expect it to change before it's final. If you don't write unsafe code today, you can safely ignore it. If you do, it's worth trying on a branch and telling the .NET team what you find.
That's all seven C# 15 features. The big ones for everyday code are union types and closed hierarchies; the rest make common code a little shorter and a little safer.
On one page: the library authors cheat sheet, to keep or to share.
Sources
- Microsoft, What's new in C# 15, Microsoft Learn: both features, and memory safety's multirelease plan.
- Microsoft, Interfaces (C# reference), Microsoft Learn: static interface members on older runtimes, and what still needs a newer runtime.
- Microsoft, Unsafe code, pointer types, and function pointers, Microsoft Learn: the updated memory safety rules.
- Microsoft, Language version (C# compiler options), Microsoft Learn: switching on the memory safety preview.
Tags: csharp, csharp-15, dotnet, whats-new