Search code examples
c#.netparams-keyword

What is a real example of when to use params as a method argument?


As I understand it, params is just syntactic sugar that "under the hood" simply gives you an array of the type you specify.

First, when would you use this?

Second, why would you use it instead of just declaring an array argument?


Solution

  • Math.Min takes exactly two arguments. This is a stupid limitation. Many other languages allow you to write:

    double x, y, z, w;
    double least = min(x, y, z, w);
    

    If you wanted to write a min function that could be used like that, you'd want to use params.