ZetCode

C# params Keyword with Collections

Last modified April 19, 2025

This tutorial explores how to use the params keyword in C# with various collection types like List<T>, Span<T>, and IEnumerable<T>.

In C# 13, the params keyword has been extended to support additional collection types beyond arrays, enabling more flexible and efficient method parameter handling.

Understanding the params Keyword in C#

The params keyword allows a method to accept a variable number of arguments, which are treated as a collection inside the method. With C# 13, params now supports types like List<T>, Span<T>, IEnumerable<T>, and others, in addition to arrays.

Key characteristics of params:

Using params with Array

This example demonstrates params with an array to calculate the average.

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

int[] values = { 4, 8, 12 };
Console.WriteLine(Average(1, 2, 3));
Console.WriteLine(Average(values));

The Average method computes the average of a variable number of integers or an array passed directly.

$ dotnet run
2
8

Using params with List<T>

This example uses params with a List<int> to sum numbers.

Program.cs
int SumNumbers(params List<int> numbers)
{
    return numbers.Sum();
}

Console.WriteLine(SumNumbers(1, 2, 3));
Console.WriteLine(SumNumbers(10, 20, 30, 40));
Console.WriteLine(SumNumbers());

The SumNumbers method accepts a variable number of integers as a List<int> and returns their sum.

$ dotnet run
6
100
0

Using params with Span<T>

This example uses params with a Span<string> to concatenate strings.

Program.cs
string ConcatStrings(params Span<string> words)
{
    return string.Join(" ", words.ToArray());
}

Console.WriteLine(ConcatStrings("Hello"));
Console.WriteLine(ConcatStrings("C#", "is", "awesome"));
Console.WriteLine(ConcatStrings());

The ConcatStrings method joins strings from a Span<string>, handling any number of inputs.

$ dotnet run
Hello
C# is awesome

Using params with IEnumerable<T>

This example combines a fixed parameter with a params IEnumerable<string> parameter.

Program.cs
void PrintItems(string category, params IEnumerable<string> items)
{
    Console.WriteLine($"Category: {category}");
    foreach (var item in items)
    {
        Console.WriteLine($"  - {item}");
    }
}

PrintItems("Fruits", new[] { "Apple", "Banana", "Orange" });
PrintItems("Tools", "Hammer", "Screwdriver");

The PrintItems method takes a category and a variable number of items as an IEnumerable<string>, printing them in a list.

$ dotnet run
Category: Fruits
  - Apple
  - Banana
  - Orange
Category: Tools
  - Hammer
  - Screwdriver

Using params with ReadOnlySpan<T>

This example demonstrates params with a ReadOnlySpan<object>.

Program.cs
void DisplayValues(params ReadOnlySpan<object> values)
{
    foreach (var value in values)
    {
        Console.WriteLine($"Value: {value}");
    }
}

DisplayValues(42, "Hello", 3.14, true);
DisplayValues("Test", 100);

The DisplayValues method accepts a variable number of objects as a ReadOnlySpan<object>, displaying each one.

$ dotnet run
Value: 42
Value: Hello
Value: 3.14
Value: True
Value: Test
Value: 100

Passing a Collection to params

This example shows passing a List<int> to a params List<int> parameter.

Program.cs
int MaxValue(params List<int> numbers)
{
    return numbers.Any() ? numbers.Max() : 0;
}

List values = [5, 2, 8, 1, 9];
Console.WriteLine(MaxValue(1, 2, 3));
Console.WriteLine(MaxValue(values));

The MaxValue method finds the maximum value from a variable number of integers or a List<int> passed directly.

$ dotnet run
3
9

Best Practices

When using params with collections in C#:

Source

params keyword - language reference

This tutorial has demonstrated how to use the params keyword in C# with various collection types for flexible method parameters.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.