Codectionary / Developer documentation / C#

Accept Variable Argument Counts with params

Use a params array for small variable-length argument lists and understand the empty-call case.

Read the syntax

Use a params array for small variable-length argument lists and understand the empty-call case.

LINQ Sum on this integer array adds them to produce 18.

What it is and why to use it

A helper may need to total two values now and five later. params lets callers supply zero or more values to a final parameter using a convenient argument-list form. The array form shown here is supported in C# 12 and earlier; newer C# versions also support additional collection forms that are not required for these examples.

How it works

Declare one params array as the final parameter. Inside the method it is an ordinary array. A call can supply individual elements or an existing array. An empty argument list supplies an empty array. Passing an existing array does not make a defensive copy, so mutations inside the helper can affect the caller’s data.

Common mistakes and limitations

Do not place another parameter after params or assume a supplied array is copied. Avoid ambiguous overload sets and unnecessary variable-argument helpers on hot paths without measurement. Explicit null arrays can still cause failures; decide and document the contract.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

static int Total(params int[] values) => values.Sum();

Examples

Handle several values or no values

Sum a variable number of integers with one helper.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Console.WriteLine(Total(4, 6, 8));
Console.WriteLine(Total());
static int Total(params int[] values) => values.Sum();
  1. The first call supplies three elements to the params array.
  2. LINQ Sum on this integer array adds them to produce 18.
  3. The second call supplies an empty array; its sum is zero. The helper’s empty-input meaning is therefore explicit.
  4. Expected output: 18 0
  5. Define what an empty call means.

Pass an existing array directly

Reuse a prepared list of values without changing the call contract.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

int[] scores = { 7, 8 };
Console.WriteLine(Total(scores));
Console.WriteLine(string.Join(", ", scores));
static int Total(params int[] values) => values.Sum();
  1. A compatible array can be supplied as the params argument itself.
  2. This helper only reads the array, so the original scores remain unchanged.
  3. The absence of mutation comes from the implementation, not an automatic read-only guarantee supplied by params.
  4. Expected output: 15 7, 8
  5. Define what an empty call means.

Best practices

  • Define what an empty call means.
  • Document whether supplied arrays may be mutated.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading