ZetCode

C# params keyword

last modified November 6, 2026

In this article we show how to use the params keyword in C# with arrays, spans, and other collection types.

The params keyword lets a method accept a variable number of arguments. The caller passes zero or more values separated by commas, and the compiler collects them into a collection. Until C# 12, the parameter had to be a single-dimensional array. C# 13 introduced params collections: the parameter can be any type that supports collection expressions, such as Span<T>, ReadOnlySpan<T>, List<T>, or IEnumerable<T>.

Rules for params parameters:

params with an array

The classic form of params uses an array.

Program.cs
Console.WriteLine(Average(1, 2, 3));
Console.WriteLine(Average(4, 8, 12, 16));
Console.WriteLine(Average());

int[] values = [10, 20, 30];
Console.WriteLine(Average(values));

double Average(params int[] numbers)
{
    return numbers.Length > 0 ? numbers.Average() : 0;
}

The Average method computes the average of any number of integers.

Console.WriteLine(Average());

Without arguments, the method receives an empty array, never null.

int[] values = [10, 20, 30];
Console.WriteLine(Average(values));

An existing array is passed as it is, without copying.

$ dotnet run
2
10
0
20

params with ReadOnlySpan

A params ReadOnlySpan<T> parameter avoids allocating an array on the heap. When the caller passes individual values, the compiler stores them in a buffer on the stack. This makes it the most efficient choice for methods that only read the arguments.

Program.cs
Console.WriteLine(Sum(1, 2, 3));
Console.WriteLine(Sum(10, 20, 30, 40));
Console.WriteLine(Sum());

int[] arr = [5, 6, 7];
Console.WriteLine(Sum(arr));
Console.WriteLine(Sum(arr.AsSpan(1)));

int Sum(params ReadOnlySpan<int> numbers)
{
    int total = 0;

    foreach (int n in numbers)
    {
        total += n;
    }

    return total;
}

The Sum method adds up the numbers in a span.

Console.WriteLine(Sum(arr));
Console.WriteLine(Sum(arr.AsSpan(1)));

An array converts to a span implicitly. We can also pass a slice of an array without copying it.

Use Span<T> instead of ReadOnlySpan<T> only if the method needs to modify the elements. Span parameters cannot be declared in async methods (error CS4012), and spans cannot be stored in fields of classes.

$ dotnet run
6
100
0
18
13

params with interfaces

When the parameter type is an interface such as IEnumerable<T>, IReadOnlyCollection<T>, or IReadOnlyList<T>, the compiler chooses a suitable implementation for the individual arguments.

Program.cs
PrintItems("Fruits", "apple", "banana", "orange");
PrintItems("Tools");

List<string> tools = ["hammer", "screwdriver"];
PrintItems("Tools", tools);

Console.WriteLine(Describe(4, 1, 3));

void PrintItems(string category, params IEnumerable<string> items)
{
    Console.WriteLine($"{category}: {string.Join(", ", items)}");
}

string Describe(params IReadOnlyList<int> vals)
{
    return $"{vals.Count} values, first {vals[0]}, last {vals[^1]}";
}

The PrintItems method has a fixed parameter followed by the params parameter. The Describe method uses the indexer of IReadOnlyList<int>.

List<string> tools = ["hammer", "screwdriver"];
PrintItems("Tools", tools);

Any collection that implements the interface can be passed directly. Interface parameters are the most flexible for callers.

return $"{vals.Count} values, first {vals[0]}, last {vals[^1]}";

The ^1 index refers to the last element.

$ dotnet run
Fruits: apple, banana, orange
Tools: 
Tools: hammer, screwdriver
3 values, first 4, last 3

params with concrete collections

Concrete collection types such as List<T> and HashSet<T> work as well. The compiler creates the collection and adds the arguments to it.

Program.cs
Console.WriteLine(Max(1, 8, 3));

List<int> values = [5, 2, 9, 1];
Console.WriteLine(Max(values));

Tags("csharp", "dotnet", "csharp", "linq", "dotnet");

int Max(params List<int> numbers)
{
    return numbers.Count > 0 ? numbers.Max() : 0;
}

void Tags(params HashSet<string> tags)
{
    Console.WriteLine($"{tags.Count} unique tags: {string.Join(", ", tags)}");
}

The Max method takes a list, and the Tags method takes a set.

void Tags(params HashSet<string> tags)

The arguments are added to a HashSet<string>, which removes the duplicates.

$ dotnet run
8
9
3 unique tags: csharp, dotnet, linq

Overload resolution

When several params overloads are applicable, the compiler prefers span types over arrays and interfaces for individual arguments. This is why many .NET 9 methods, such as string.Join, string.Concat, Path.Combine, and Task.WhenAll, gained params ReadOnlySpan<T> overloads: existing code becomes faster after recompilation.

Program.cs
Printer.Show(1, 2, 3);
Printer.Show([1, 2, 3]);

int[] arr = [4, 5];
Printer.Show(arr);

List<int> list = [6, 7, 8, 9];
Printer.Show(list);

static class Printer
{
    public static void Show(params int[] vals) =>
        Console.WriteLine($"array: {vals.Length} items");

    public static void Show(params ReadOnlySpan<int> vals) =>
        Console.WriteLine($"span: {vals.Length} items");

    public static void Show(params IEnumerable<int> vals) =>
        Console.WriteLine($"enumerable: {vals.Count()} items");
}

The Printer class has three Show overloads. Local functions cannot be overloaded, so we use a static class.

Printer.Show(1, 2, 3);
Printer.Show([1, 2, 3]);

Individual values and a collection expression bind to the span overload.

Printer.Show(arr);

An existing array matches the array overload exactly, so that one is called.

Printer.Show(list);

A list converts only to IEnumerable<int>.

$ dotnet run
span: 3 items
span: 3 items
array: 2 items
enumerable: 4 items

Collection expressions as arguments

A collection expression can be passed to a params parameter. With the spread operator .., it combines several collections into one argument.

Program.cs
int[] a = [1, 2, 3];
List<int> b = [4, 5];

Console.WriteLine(Join(a));
Console.WriteLine(Join([.. a, .. b, 6]));

string Join(params ReadOnlySpan<int> vals) => string.Join(" ", vals.ToArray());

We pass an array and a combination of an array, a list, and a value.

Console.WriteLine(Join([.. a, .. b, 6]));

The spread operator is allowed only inside the collection expression; it cannot be used among individual params arguments.

$ dotnet run
1 2 3
1 2 3 4 5 6

params with objects

A params ReadOnlySpan<object> parameter accepts values of any type.

Program.cs
DisplayValues(42, "hello", 3.14, true, 'c');
DisplayValues();

Console.WriteLine(string.Concat("a", "b", "c", "d", "e"));
Console.WriteLine(Path.Join("home", "user", "docs", "report.txt"));

void DisplayValues(params ReadOnlySpan<object> values)
{
    Console.WriteLine($"{values.Length} values:");

    foreach (var value in values)
    {
        Console.WriteLine($"  {value} ({value.GetType().Name})");
    }
}

The DisplayValues method prints the values and their types. We also call two .NET methods with params parameters.

Console.WriteLine($"  {value} ({value.GetType().Name})");

Value types such as int and bool are boxed when they are stored as object. Prefer specific or generic types when possible.

$ dotnet run
5 values:
  42 (Int32)
  hello (String)
  3.14 (Double)
  True (Boolean)
  c (Char)
0 values:
abcde
home/user/docs/report.txt

Best practices

Source

params modifier - language reference

In this article we have used the params keyword with arrays, spans, and collections in C#.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C# tutorials.