Microsoft released the latest version of C# i.e. C#13 and now it allows params to be any of the collections supported by collection expressions rather than just arrays. So, I quickly wrote the benchmark code to test the performance and overall, it did improve the performance.
Here’s the code snippet and results for the single value test:
public class ParamsBenchmark{
// Dummy methods to illustrate the params collections
public int SumOld(params int[] numbers)
{
var sum = 0;
foreach (var num in numbers)
{
sum += num;
}
return sum;
}
public int SumNew(params Span<int> numbers)
{
int sum = 0;
foreach (var num in numbers)
{
sum += num;
}
return sum;
}
[Benchmark]
public int SumOldParams()
{
return SumOld(1, 2, 3, 4, 5);
}
[Benchmark]
public int SumNewParams()
{
return SumNew([1, 2, 3, 4, 5]);
}
}
Below are my benchmark results: