The article is suitable for anyone and everyone as long as you know what a method and an array is in C# !
Outcomes
After reading this you should have a fairly good grasp on the Where, When and Why on using the params
keyword for a method parameter, and possibly by the end of this it should have skyrocketed into your top 3 favorite method parameter types.
Happy reading ! 📚📚
What is it? ❔
The params
keyword is , simply put, a method parameter that can take a variable number of arguments - you can pass in anything from one to infinity or even none at all !! It doesn't matter !!
Why would we need to do this ? 🤔
Sometimes we just don't know the answers in life, and this can apply to the number of arguments in a method.
Using the params
keyword we can pass in multiple or none depending on our needs at the time. With this we can avoid having to write multiple overloads for our methods like the below example.
// Multiple Overloads
static int AddAll(int a, int b)
{
return a + b;
}
static int AddAll(int a, int b, int c)
{
return a + b + c;
}
static int AddAll(int a, int b, int c, int d)
{
return a + b + c + d;
}
When using the params
keyword you can just write
// Method
static int AddAll(params int[] list)
{
int total = 0;
for (int i = 0; i < list.Length; i++)
{
total += list[i];
}
return total;
}
// Method Call
int sum0 = AddAll();
int sum2 = AddAll(2,5);
int sum3 = AddAll(2,5,7);
// Or Declare the array and pass it in
int[] nums = { 1,3,4,8 };
int sum4 = AddAll(nums);
As you can see this gives us the option to pass in none, and is also much more concise and clean than overloading.
How Does it Work ? 🔨
To start off you just set the method parameter like
static int AddAll(params int[] list){....}
Calling it is pretty simple really - there are two options:
- Pass values in separated by commas e.g.
int sum3 = AddAll(2,5,7);
These will then be converted into a temporary array by the compiler, and if no arguments are given an empty array will still be created anyway.
- Create an array and then pass that into the method.
int[] nums = { 1,3,4,8 };
int sum4 = AddAll(nums);
There are some quirks however that you should be aware of -
- There can only be one parameter of
param
type per method. - The
param
type must be the last parameter. e.g.
static int AddAll(string name, params int[] list)
This will work because the params
is the last argument.
However if we reverse this like so
static int AddAll(params int[] list, string name)
This will not work.
- Optional parameters and the
params
type will not work together. - The
params
must take single-dimensional array - a multi-dimensional one will throw an error.
Farewell Speech 👋🎺
So that's all folks, I hope this has cleared up any doubts you may have had about the params
keyword.
You can read the Microsoft docs here
Feel free to ask questions, comment or contribute below!
And if you're feeling generous you can buy me a coffee with the link below ( and yes its all for coffee, I drink a copius amount of it while writing ☕ )
Top comments (1)
I am not if this holds true in Core (or net 6 for that matter), but there was some runtime overhead in Net Framework.
Just keep this in mind, if microseconds count due to very very many calls during execution.