C
C#2y ago
swilli

❔ Repeating parameters vs variadic functions

Does anybody know why the params keyword is not used more commonly? This example if from the ImmutableArray<> class. As you can see there's methods for creating an array from 1-4 args, and then it goes to an array arg, but the array method is then using the params keyword? Why not just use the params keyword on the first method so that it could take N args without needing multiple methods with differing amounts of parameters? I feel like I'm missing something here.
9 Replies
TheBoxyBear
TheBoxyBear2y ago
Don't quote me but iirc arrays are stored in a different memory that is a lot more expensive than passing simple values as parameters In practice it's unoticable, programs use arrays all the time
swilli
swilliOP2y ago
This is probably a good chance for me to do a little benchmarking and see what the performance difference between the two method signatures
TheBoxyBear
TheBoxyBear2y ago
But it can make a difference in performance critical situations
333fred
333fred2y ago
Yes, it's for performance Much cheaper to pass 3 parameters than to make an array
swilli
swilliOP2y ago
Benchmark that demonstrates Fred's point. The One through Four have the explicit definitions from above, five is where it becomes variadic
333fred
333fred2y ago
Turn on memory benchmarking too
Anton
Anton2y ago
I saw an issue on span params, this should be resolved when they add those
swilli
swilliOP2y ago
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server