Using the Params Keyword in C#
Tags: csharp, dotnet, programming
Published: 2021-12-29 13:16:58

>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. 

```csharp
// 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
 
```csharp
// 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 
```csharp
static int AddAll(params int[] list){....}
```

Calling it is pretty simple really - there are two options:
1. Pass values in separated by commas 
e.g.
```csharp
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.

2. Create an array and then pass that into the method.  
```csharp
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. 
```csharp
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 
```csharp
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](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params)

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 ☕ )  


[![Buy Me A Coffee](https://cdn.buymeacoffee.com/buttons/default-black.png)](https://www.buymeacoffee.com/SUKaQp9)