The params
modifier keyword could only take the supplied arguments and put them into an array.
With C# 13 the params
keyword can now be used with other collection types such as List<T>
, Span<T>
, IEnumerable<T>
etc.
Code
C#
var results = SquareList(1, 2, 3, 4, 5);
// ...
public List<int> SquareList(params List<int> numbers)
=> list.ConvertAll(x => y * y);
C#
var results = SquareList(1, 2, 3, 4, 5);
// ...
public List<int> SquareList(params int[] numbers)
=> new List<int>(numbers).ConvertAll(x => y * y);
Notes
- If you specify an interface type, the compiler will choose an appropriate collection type to use.