How it works
A params parameter lets a caller supply a variable number of arguments. An array-based parameter array appears last in the parameter list, and the method receives it as an ordinary array. The call can contain separate values, an existing array, or no values at all. This makes a compact API for operations such as combining labels or totaling a few measurements.
The convenient call syntax does not remove the need for an empty-input policy. Addition has a natural identity of zero, so a total of no values can be zero. An average does not have the same simple answer. Decide the contract of the operation first, then use the parameter-array syntax as a way to express that contract at the call site.
Walk through the example. The Total function loops over its parameter array and accumulates the result. With no arguments, the loop has no iterations and returns zero. Supplying three separate integers produces six. Passing an existing array uses the same calculation. The function reads its input and does not modify the caller's array.
Watch the boundary. An existing array passed to an array-based params parameter is still the same array object; the method must not assume it owns a private copy. Writing into it can surprise the caller. Also avoid adding many overloads whose combinations make an empty or null call ambiguous. A convenient API should remain predictable when its smallest possible input is supplied.
Practice independently. Implement a JoinLabels function with a required separator followed by a string parameter array. Define the output for zero labels and one label. Test an existing array and several separate string arguments. Then create a version that filters blank labels, and document whether blank values are ignored or preserved. Check that the original input array remains unchanged after every call.
A working example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
int Total(params int[] values)
{
int sum = 0;
foreach (int value in values) sum += value;
return sum;
}
Console.WriteLine(Total());
Console.WriteLine(Total(1, 2, 3));
int[] batch = { 4, 5 };
Console.WriteLine(Total(batch));
Console.WriteLine(string.Join(",", batch));
Expected output
0 6 9 4,5
Define the zero-argument case and avoid mutating arrays owned by callers.