Skip to main content

How to Pass Any Number of Arguments to a Method with params in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

With params, one method takes any number of arguments. Total(), Total(5) and Total(5, 10, 20) all call the same method, which gets the numbers as one array.

Microsoft's guide to methods: By using the params keyword to indicate that a parameter is a parameter collection, you allow your method to be called with a variable number of arguments. The parameter tagged with the params keyword must be a collection type, and it must be the last parameter in the method's parameter list.

Syntax​

Syntax
<ReturnType> <MethodName>(<otherParameters>, params <ElementType>[] <name>)

<MethodName>(<otherArguments>, <value1>, <value2>, <value3>);
PartWhat it is
<otherParameters>Any ordinary parameters. They come first, and there can be none
paramsLets the caller pass any number of values for the last parameter, or none at all
<ElementType>[]An array of the values' type, such as int[]. Since C# 13 it can also be a collection type, such as IEnumerable<string>
<name>Inside the method, the array or collection that holds every value passed for it
<value1>, <value2>, <value3>The values, separated by commas. A ready-made array works too

The reference adds: You can only use one params keyword in a method declaration.

The Example​

A console app called ParamsArgs has a Total method that adds up any number of whole numbers and says how many it got, and a Sentence method that joins any number of words.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n ParamsArgs
cd ParamsArgs
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/ParamsArgs/ParamsArgs.csproj:
Determining projects to restore...
Restored /Users/you/source/ParamsArgs/ParamsArgs.csproj (in 73 ms).
Restore succeeded.

dotnet new console -n ParamsArgs creates a folder named ParamsArgs with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or ParamsArgs.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First, three calls with separate numbers:

Program.cs (separate arguments)
// One method, called with any number of numbers.
Console.WriteLine(Total());
Console.WriteLine(Total(5));
Console.WriteLine(Total(5, 10, 20));

Total() passes no numbers, Total(5) passes one and Total(5, 10, 20) passes three. The compiler puts them into an array for you.

Then a call with an array:

Program.cs (an array)
// An array you already have is passed as it is.
int[] prices = [3, 4, 5];
Console.WriteLine(Total(prices));

prices is already an int[], so Total gets that array.

Then a call to a method with a params collection:

Program.cs (a params collection)
// params also works with collection types such as IEnumerable<T> (C# 13 and later).
Console.WriteLine(Sentence("Tea", "is", "ready"));

The call looks the same as for an array: just the words, separated by commas.

Last, the two methods:

Program.cs (the methods)
// params collects the arguments into one array.
static string Total(params int[] numbers) => $"count {numbers.Length}, total {numbers.Sum()}";

static string Sentence(params IEnumerable<string> words) => string.Join(" ", words) + ".";
  • params int[] numbers: inside Total, numbers is an ordinary int[]. numbers.Length is how many numbers came in, and numbers.Sum() adds them up.
  • params IEnumerable<string> words: a params collection. C# 13 made this possible: The params modifier isn't limited to array types. string.Join(" ", words) puts a space between the words.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
count 0, total 0
count 1, total 5
count 3, total 35
count 3, total 12
Tea is ready.

macOS-style terminal window titled Terminal — ParamsArgs showing dotnet run printing count 0, total 0, count 1, total 5, count 3, total 35, count 3, total 12 and Tea is ready.

One Total method handled every call: no numbers gave a total of 0, one number 5, three numbers 35, and the array of three 12. With no arguments, numbers wasn't null but an empty array, so numbers.Length was 0. The reference: If you send no arguments, the length of the params list is zero.

The Complete File​

macOS-style code window titled Program.cs — ParamsArgs showing Total called with no numbers, one number, three numbers and an int array, Sentence called with three words, and the two methods: Total with a params int array that returns the count and the sum, and Sentence with a params IEnumerable of string that joins the words

Program.cs
// One method, called with any number of numbers.
Console.WriteLine(Total());
Console.WriteLine(Total(5));
Console.WriteLine(Total(5, 10, 20));

// An array you already have is passed as it is.
int[] prices = [3, 4, 5];
Console.WriteLine(Total(prices));

// params also works with collection types such as IEnumerable<T> (C# 13 and later).
Console.WriteLine(Sentence("Tea", "is", "ready"));

// params collects the arguments into one array.
static string Total(params int[] numbers) => $"count {numbers.Length}, total {numbers.Sum()}";

static string Sentence(params IEnumerable<string> words) => string.Join(" ", words) + ".";

Common Mistakes​

MistakeWhat happensDo this instead
params that isn't the last parameterTotal(params int[] numbers, string label) didn't build: error CS0231: A params parameter must be the last parameter in a parameter listPut it last: Total(string label, params int[] numbers) built, and Total("Sum", 5, 10, 20) printed Sum: 35
params on a single valueparams int number didn't build: error CS0225: The params parameter must have a valid collection typeUse an array, params int[] numbers, or a collection type
Passing a List<int> to params int[]Total(prices) with a List<int> didn't build: error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<int>' to 'int'Spread the list into the call: Total([.. prices]) printed count 3, total 12. Or declare params IEnumerable<int>, which took the list as it is

FAQ​

Can a method have two params parameters?​

No. Show(params int[] numbers, params string[] words) didn't build: the first one isn't the last parameter, so the compiler reported error CS0231 again. Keep params on the last parameter only: with Show(int[] numbers, params string[] words), the call Show([1, 2], "a", "b", "c") printed 2 numbers, 3 words.

Which collection types can params use?​

Before C# 13, arrays only: Before C# 13, you must use a single-dimensional array for the parameter. Since C# 13, the release notes say You can now use params with any recognized collection type, and name Span<T>, ReadOnlySpan<T>, and types that implement IEnumerable<T> and have an Add method, such as List<T>. Interfaces such as IEnumerable<T> work too, as Sentence showed.

Conclusion​

Mark a method's last parameter with params when callers should be able to pass any number of values. They list the values separated by commas, or pass a ready-made array, and the method gets them as one collection, which is empty when they pass none.

Additional Resources​