Skip to content

EN CSharp Keyword params

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

C#: params

Handbook C# · Practical Guide

Allows calling with different number of arguments; passes an array or a supported collection-form. Is the last parameter of the method.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
int Sum(params int[] values)
{
    int total = 0;
    foreach (int value in values) total += value;
    return total;
}
System.Console.WriteLine(Sum(1, 2, 3));

Result:

6

Syntax

Method(params int[] values) / Method(params ReadOnlySpan<int> values)

params collections are available from C# 13. The massive form exists in earlier versions of the language.

Example 1: simple form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Console.WriteLine(Numbers.Sum(1, 2, 3));
        Console.WriteLine(Numbers.Sum(new int[] { 4, 5 }));
    }
}
public static class Numbers { public static int Sum(params int[] values) => values.Sum(); }

Example 2: another practical form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Console.WriteLine(Numbers.Sum(2, 4, 6));
    }
}
public static class Numbers { public static int Sum(params ReadOnlySpan<int> values) { int sum = 0; foreach (int value in values) sum += value; return sum; } }

Official source: Microsoft Learn.

Clone this wiki locally