C# 13: Use params with any recognized collection type
The params
modifier in C# has traditionally been associated with array types, allowing methods to accept a variable number of arguments. However, starting with C# 13, you can now use params
with a variety of collection types, broadening its applicability and making your code even more versatile.
Supported Collection Types
The params
modifier now works with several recognized collection types, including:
System.Span<T>
System.ReadOnlySpan<T>
- types implementing
System.Collections.Generic.IEnumerable<T>
that also have anAdd
method.
Additionally, you can use params
with the following system interfaces:
System.Collections.Generic.IEnumerable<T>
System.Collections.Generic.IReadOnlyCollection<T>
System.Collections.Generic.IReadOnlyList<T>
System.Collections.Generic.ICollection<T>
System.Collections.Generic.IList<T>
A Practical Example: Using Spans with params
One of the exciting possibilities with this enhancement is the ability to use spans as params
parameters. Here’s an example:
public void Concat<T>(params ReadOnlySpan<T> items)
{
for (int i = 0; i < items.Length; i++)
{
Console.Write(items[i]);
Console.Write(" ");
}
Console.WriteLine();
}
Code language: C# (cs)
In this method, params
enables you to pass a variable number of spans into the Concat
method. The method processes each span in sequence, demonstrating the enhanced flexibility of the params
modifier.
Compiler Behavior and Overload Resolution
The introduction of params
collections mean adjustments in compiler behavior, particularly concerning overload resolution. When a method includes a params
parameter of a non-array collection type, the compiler evaluates the applicability of both the normal and expanded forms of the method.
Error Handling and Best Practices
Whenever using params
, it’s important to adhere to best practices in order to prevent common errors:
- parameter positioning – ensure that the
params
parameter is the last in the formal parameter list - modifier restrictions – avoid combining
params
with modifiers such asin
,ref
, orout
- default values – do not assign default values to
params
parameters, as this is not permitted