Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The params keyword

Reference for C# params: accept any number of arguments as an array, Span or collection, expanded versus normal form, rules, overloads and allocation costs.

params marks the last parameter of a method as a parameter collection. Callers can then pass any number of separate arguments, including none, and the compiler packs them into a collection for you. Console.WriteLine(string, params object[]) and string.Join(string, params string[]) are familiar examples.

Classically the parameter must be a one-dimensional array. Since C# 13 it can also be a Span<T>, ReadOnlySpan<T>, or any type that supports collection expressions, such as List<T> or IEnumerable<T>.

C#
static int Sum(params int[] numbers) => numbers.Sum();
Sum();           // 0
Sum(1, 2, 3);    // 6

Try it

C# Passing values, an array, or nothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# params with ReadOnlySpan and List (C# 13)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
Type Method(T1 a, params T[] rest)
Type Method(params ReadOnlySpan<T> rest)                // C# 13
Type Method(params IEnumerable<T> rest)                 // C# 13
Type Method(params List<T> rest)                        // C# 13
Rule Description
Position params can be applied only to the last parameter, and only one parameter per method.
Type An array of one dimension, or (C# 13) a type with a collection-expression-compatible shape: Span<T>, ReadOnlySpan<T>, IEnumerable<T>, IReadOnlyList<T>, List<T> and so on.
Modifiers Cannot be combined with ref, out, in or a default value.
Normal form The caller passes one argument of the parameter's own type; it is used as the collection.
Expanded form The caller passes zero or more separate arguments; the compiler builds a collection.

Errors: a params parameter that is not last is CS0231; params with a type that is not a valid collection type is CS0225; params ref and params out give CS1611; a default value on a params parameter is CS1751.

Usage notes

  • Zero arguments are legal. The method receives an empty collection, never null, in the expanded form. In normal form a caller can pass null explicitly, so validate if it matters.
  • Overload resolution. Methods that apply in normal form beat those that apply only in expanded form. Sum(ready) above calls the array directly rather than wrapping it. A fixed-parameter overload such as Sum(int a, int b) wins over params for Sum(1, 2).
  • Allocation. Before C# 13 every expanded call allocates an array. Use a params ReadOnlySpan<T> parameter on hot paths: the compiler can place the elements in a stack buffer.
  • Arrays are shared in normal form. If the callee modifies the array, the caller sees it. Copy it if you keep it.
  • object[] trap. Passing a single string[] to params object[] is passed in normal form (array covariance), not as one element. Passing an int[] is a single element, because int[] is not an object[].
  • null ambiguity. A bare null argument is treated as a null array, not an array with one null element. Write (object)null to get the latter.
  • Prefer overloads for the common small counts in libraries, as the BCL does for Path.Combine, and use params for the open-ended case.
  • Collection expressions are an alternative at the call site: Sum([1, 2, 3]) passes one collection argument without params being involved.
  • Lambdas can declare a params array parameter since C# 12: var f = (params int[] xs) => xs.Length;.