Monday, July 29, 2024

C# new params collections C# 13

 

C# 13

C# 13 focuses on flexibility and performance, making many of your favorite features even better. Enhancing params parameters are to provide you with more flexibility. 

Let’s take a look!

Enhancing C# params

params are no longer restricted to arrays! 

When the params keyword appears before a parameter, calls to the method can provide a comma delimited list of zero or more values and those values are placed in a collection of the parameter’s type. Starting in C# 13, the params parameter type can be any of the types used with collection expressions, like List<T>Span<T>, and IEnumerable<T>. What are the benefits of these overloads? By adding an IEnumerable<T> overload, support for LINQ is enabled. And by adding a ReadOnlySpan<T> or Span<T> overload, memory allocations can be reduced, enhancing performance. 

Just specify a different collection type as the parameter type:

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"

It’s really that easy to use the collection type that best fits your needs. Programmers using your method can just pass a comma delimited list of values. They do not need to care about the underlying type.

Making params better with spans

One important aspect of performance is reducing memory use, and System.Span<T> and System.ReadonlySpan<T>are tools in reducing memory allocations. You can learn more in Memory and Span usage guidelines.

If you want to use a span, just use the params parameter type to a span type. Values passed to the params parameter are implicitly converted to that span type. If you have two method signatures that differ only by one being a span and the other being an array and the calling code uses a list of values, the span overload is selected. This means you’re running the fastest code available and makes it easier to add span to your apps.

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. Here is an example from StringBuilder.

public StringBuilder AppendJoin(string? separator, params ReadOnlySpan<string?> values)

params and interfaces

The story gets even better with params support for interfaces. If no concrete type is specified, how does the compiler know what type to use?

Just like collection expressions in C# 12, when you specify an interface as a parameter type, it’s a clear indication that you just want anything that implements that interface. Key interfaces are mapped to implementation, so we can give you the best available type that fulfills the interface. The compiler may use an existing type or create one. You should not have any dependencies on the underlying concrete collection type because we will change it if a better type is available.

The great thing about this design is that you can just use interfaces for your params types. If you pass a value of a type that implements the interface, it will be used. When a list of values or a collection expression are passed, the compiler will give you the best concrete type.

No comments:

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...