Discover the C# 13 new features in this two-part series. C# 13 is supported by the .NET 9 SDK, which was released in November 2024. The features described in this article are things you’ll find in everyday programs. In the second article, we’ll cover more specialized features.
Params collectionsParams collections
The C# params
modifier enables a method to accept an arbitrary number of arguments of a certain type. For example, this code
Console.WriteLine("{0} + {1} + {2} = {3}", i, j, k, sum);
calls the following WriteLine
params
overload:
static void WriteLine (string format, params object?[]? arg);
The params
argument must be the last argument of the method. Previously the compiler required it to be an array type. With C# 13, the compiler allows collection types. This means the type can be a Span<T>
, ReadOnlySpan<T>
, IEnumerable<T>
, (IReadOnly)Collection<T>
, (IReadOnly)List<T>
, or any type that implements IEnumerable<T>
and has an Add
method that accepts the item type. The compiler generates optimized code based on the provided arguments and the params
target type.
.NET 9 leverages params
collections. For example, Console.WriteLine
has a new overload that accepts the arg
as a ReadOnlySpan
:
static void WriteLine (string format, scoped ReadOnlySpan<object?> arg);
For our initial example, the compiler calls this new overload and rather than allocating an array (on the heap), it will construct the ReadOnlySpan
on the stack instead.
Partial properties and indexers
C# 13 adds support for partial
properties and indexers. Like C# 9’s partial
methods, this allows to split the declaration and implementation of the property (or indexer) in separate files.
The main use-case for this is to let the user define a property and have a source-generator provide the implementation.
When the partial
keyword is used, lack of a body is treated as the property declaration. To implement the property, the accessor body must be explicitly included.
partial class C
{
// Declare property of type 'string' with a getter.
public partial string Property { get; }
}
// -- Other file, probably generated by a source generator.
partial class C
{
private string _value;
// Provide implementation of the property.
public partial string Property { get => _value; }
}
Implicit index access in object initializersImplicit index access in object initializers
C# 8 introduced support for ranges and indexes. This enables us to index elements from the end of a collection using the ^
operator. This operator can now also be used in object initializers, as shown in the next example.
var buffer = new Buffer()
{
Data =
{
[^1] = 10, // Initialize the last element to '10'.
}
};
sealed class Buffer
{
public byte[] Data { get; } = new byte[256];
}
Note that in the object initializer, the Data =
does not represent an assignment of the property. It initializes the value returned by the getter using the initializer block.
ref and unsafe in iterators and async methods
C#’s support for async
methods and implementing iterators using the yield
keyword rely on the compiler generating state machines that run through different parts of the user’s code based on the current state of that state machine. Consequently, the user code isn’t running once as a single function on the call stack, but it runs as part of separate function calls.
C#’s support for unsafe
and ref
features depend on code blocks that run as in a single call stack function. For example: a ref struct
is a structure that is stored on the stack and therefore the stack can not change while a ref struct
is used.
To enforce this, the C# compiler disallowed the use of unsafe
and ref
in async
methods and iterator implementations. To use these features from an async/iterator method, we need to call into separate non-async/iterator methods.
With C# 13, the compiler is now allowing the use of unsafe
and ref
in iterators and async methods as long as the code section they are used in will be part of the same stack function. For async
methods, this means we can’t cross an async
keyword; and for iterator methods, it means we can’t cross a yield
.
The following example shows the initializing of a ReadOnlySpan
(which is a ref struct
) in an async
method. This code is disallowed in previous C# versions.
int bytesRead = await stream.ReadAsync(buffer);
ReadOnlySpan<byte> received = buffer.AsSpan(0, bytesRead);
if (received.Length ...
What's next?What's next?
In this article, we looked at params
collections, which extend the usage of C#’s params
keyword. We also discussed partial properties and indexers and how these can be used by a source generator. Finally we covered the use of ref
and unsafe
in async
methods and iterator methods, and the use of the ^
operator in object initializers. In both cases, the compiler allows using these features in more places.
Stay tuned for the second article coming next week. We’ll look at the advanced features introduced in C# 13.