BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News What's New in C# 13: Enhanced Params, Performance Boosts, and New Extension Types

What's New in C# 13: Enhanced Params, Performance Boosts, and New Extension Types

Last week, during the Microsoft Build 2024, Microsoft announced the new preview features of C# 13, the latest version of the popular .NET programming language. As the most notable improvements to params parameters, the new extension types are announced, and the release includes several performance and memory enhancements for .NET developers.

In C# 13, the params keyword is no longer limited to arrays. When used before a parameter, params allow a method to accept a comma-separated list of zero or more values, which are placed in a collection of the specified type.

Now, the params parameter type can be any collection type compatible with collection expressions, such as List<T>, Span<T>, and IEnumerable<T>. It is stated that custom collection types can also be used if they follow specific guidelines.

void PrintList(params IEnumerable<string> list) 
    => Console.WriteLine(string.Join(", ", list));

PrintList("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

// prints "Sun, Mon, Tue, Wed, Thu, Fri, Sat"

Furthermore, performance enhancements were a key focus in C# 13, particularly with the use of System.Span<T> and System.ReadOnlySpan<T>, which as stated helps reduce memory allocations.

Now, the values passed to the params parameter are implicitly converted to the span type, which as reported ensures the most efficient method is selected, especially when overloaded methods differ by span or array usage.

The official blog post states the following:

Many of the methods of the .NET Runtime are being updated to accept params Span<T>, so your applications will run faster, even if you don’t directly use spans. This is part of our ongoing effort to make C# faster and more reliable. It’s also an example of the attention we give to ensuring various C# features work well together.

C# 13 also enhances params support for interfaces. When an interface is specified as a parameter type, it indicates a requirement for any implementing type. The compiler can choose the best available type that fulfils the interface, potentially using an existing type or creating one as needed.

As explained, this design ensures that dependencies on the underlying concrete collection type are minimized, allowing for flexibility and optimization. The compiler's ability to select the optimal concrete type when a list of values or a collection expression is passed further enhances the efficiency and adaptability of the code.

The announcement of the future addition is related to Extension types which in C# 13 will be expanded by providing additional methods, properties, and members to underlying types. These extension types can be implicit, applying to all instances of the underlying type, or explicit, applying only to instances explicitly converted to the extension type.

The original announcement blog post received a few interesting ideas about improvements and suggestions for C# language, and based on different threads and forums overall community feedback is positive, with a note and written excitement about Extension types, which seems to be a liked feature by the community.

The official Microsoft Build website also publicised the session recording with the title What's New in C# 13 and it is highly recommended that developers watch it. Other C# additions are related to the new lock object, new escape sequence, method group natural type and Implicit index access

Lastly, readers can find more about the available C# 13 features on the official language documentation page.

About the Author

Rate this Article

Adoption
Style

BT