Since C# 13, what does the compiler build for calls to `params ReadOnlySpan<int>`, `params IEnumerable<int>` and `params List<int>` parameters, and what does each cost?
answer
- rules borrowed from collection expressions
- a span over a stack buffer
- read-only interfaces get a wrapper
- List<int> built with Add
- C# 12 rejects the declaration
basics
~20 sSince C# 13, params ReadOnlySpan<int> gets a span over a buffer on the caller's stack, so no heap allocation; params IEnumerable<int> gets a compiler-generated read-only collection; params List<int> gets a new List<int> filled with Add.
solid answer
~40 sC# 13 lets `params` take any type a collection expression can target: arrays, `Span<T>`, `ReadOnlySpan<T>`, the generic collection interfaces, and types with a parameterless constructor and an `Add` method. An expanded call is built as if its arguments were written as a collection expression. For `ReadOnlySpan<int>` the caller fills an inline array on its own stack, or points at static data when every element is a constant, so nothing is allocated on the heap. `IEnumerable<int>`, `IReadOnlyCollection<int>` and `IReadOnlyList<int>` receive a compiler-synthesized read-only collection; `ICollection<int>`, `IList<int>` and `List<int>` receive a `List<int>`. A single argument that already has the collection type is passed through unchanged. Under C# 12 the declaration is CS9202, and a default value is still CS1751.
code
csharp · 31 linesTotals.Describe();
Totals.Describe(1, 2, 3);
Totals.Collect(1, 2, 3);
int total = Totals.Sum(0, 2, 3) + Totals.SumArray(0, 2, 3);
long before = GC.GetAllocatedBytesForCurrentThread();
for (int i = 0; i < 1000; i++) total += Totals.Sum(i, 2, 3);
long spanBytes = GC.GetAllocatedBytesForCurrentThread() - before;
before = GC.GetAllocatedBytesForCurrentThread();
for (int i = 0; i < 1000; i++) total += Totals.SumArray(i, 2, 3);
long arrayBytes = GC.GetAllocatedBytesForCurrentThread() - before;
Console.WriteLine($"1000 calls: span {spanBytes} bytes, array {arrayBytes} bytes");
static class Totals
{
public static int Sum(params ReadOnlySpan<int> values)
{
int sum = 0;
foreach (int v in values) sum += v;
return sum;
}
public static int SumArray(params int[] values) => Sum(values);
public static void Describe(params IEnumerable<int> values) =>
Console.WriteLine($"IEnumerable<int> received {values.GetType().Name}");
public static void Collect(params List<int> values) =>
Console.WriteLine($"List<int> received {values.GetType().Name} with {values.Count} items");
}go deeper
Recall that since C# 13 params is no longer limited to arrays and can take spans, lists and collection interfaces.
Explain what the compiler builds for each parameter type, what an empty or single-collection call passes, and why the span version allocates nothing.
Show when a span parameter is the right choice and when ref struct rules force IEnumerable<T> or a copy, and how recompiling against .NET 9 changes allocation.
Weigh adding params span overloads to a shared library: callers gain only after recompiling, and older compilers cannot use the expanded form.
## What C# 13 changed Before C# 13, a `params` parameter had to be a single-dimensional array, so every call with arguments allocated a new array. **Params collections**, added in C# 13 (.NET 9), let the `params` modifier sit on any type that a **collection expression** (the `[a, b, c]` syntax from C# 12) can produce: - `Span<T>` and `ReadOnlySpan<T>`; - the interfaces `IEnumerable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `ICollection<T>` and `IList<T>`; - a class or struct that implements `IEnumerable`, has a parameterless constructor and an instance `Add` method taking one argument, such as `List<T>` or `HashSet<T>`; - a type with a collection builder `Create` method, such as `ImmutableArray<T>`. The rule from the language proposal is that an expanded call builds the collection "as though the arguments were used as expression elements in a collection expression". So a params parameter costs exactly what the equivalent collection expression costs. ## What each parameter type receives Running a small aggregation helper with the .NET 10 SDK shows the runtime types: | Parameter | What `F(1, 2, 3)` passes | Heap cost per call | |---|---|---| | `params int[]` | a new `int[]` | one array | | `params ReadOnlySpan<int>` | a span over an inline array in the caller's frame | none | | `params IEnumerable<int>` | a compiler-synthesized `<>z__ReadOnlyArray<int>` | wrapper plus array | | `params IList<int>` | a `List<int>` | list plus its storage | | `params List<int>` | a new `List<int>` filled by `Add` | list plus its storage | With **no arguments**, a `params IEnumerable<int>` receives an empty `int[]`, and a `params List<int>` receives a new, empty list. With **one argument of the parameter's own type**, such as an existing `List<int>`, the call uses the parameter's normal form and passes that object through unchanged. For span parameters, the Roslyn lowering has two strategies. For a `ReadOnlySpan<T>` whose elements are all constants of a primitive type, it can point the span at static data with `RuntimeHelpers.CreateSpan`. Otherwise, on a runtime that supports inline arrays (.NET 8 and later), it declares a fixed-size inline array as a local, fills it and passes a span over it. Either way the garbage collector sees nothing. In the example below, 1,000 calls to the span version allocated 0 bytes while 1,000 calls to the array version allocated 40,000 bytes. ## Rules that still apply - The parameter must still be last (CS0231) and cannot have a default value (CS1751): omitting the arguments already creates an empty collection. - It cannot be `ref`, `out` or `in` (CS1611). - The type must be constructible as a collection expression. `params Dictionary<string, int>` is CS9215 because its `Add` takes two arguments, and `params Stack<int>` fails with CS0117 because `Stack<T>` has `Push`, not `Add`. - Under `<LangVersion>12</LangVersion>`, declaring `params ReadOnlySpan<int>` is CS9202, "Feature 'params collections' is not available in C# 12.0". - A span parameter follows the ref struct rules: the method cannot store it in a field (CS8345) and an `async` method cannot declare it (CS4012). A method that must keep the values copies them, for example with `ToArray()`. ## The library side .NET 9 ships more than 60 methods with a `params ReadOnlySpan<T>` parameter, some new overloads and some existing span methods newly marked `params`, such as `string.Join(string? separator, params ReadOnlySpan<string?> value)` next to the older `params string?[]` overload. The .NET 9 release notes point out that existing calls like `string.Join(", ", a, b, c)` pick up the span overload, and lose the array allocation, simply by being recompiled against .NET 9 or later. Which of two such overloads wins is a question for the lesson on overload resolution; the point here is that the gain arrives at compile time, in the caller. ## Choosing the parameter type 1. Use `params ReadOnlySpan<T>` for synchronous helpers that only read the values, especially on hot paths. 2. Use `params IEnumerable<T>` or `params IReadOnlyList<T>` when the method may keep the values or is `async`; the caller-side allocation is the price. 3. Use a concrete type such as `params List<T>` only when the method genuinely needs that type; it is the most expensive choice. 4. Keep an array overload only if older compilers must still call the method in expanded form: a C# 12 caller can pass a span or an array, but its comma-separated call against a non-array params parameter is CS1501, "No overload for method 'Sum' takes 3 arguments".
- Why does params Stack<int> fail to compile while params HashSet<int> works?A non-array params type must be buildable like a collection expression: a parameterless constructor plus an instance `Add` callable with one argument. `HashSet<int>` has `Add(int)`. `Stack<int>` only has `Push`, so the compiler reports CS0117, "'Stack<int>' does not contain a definition for 'Add'". `Dictionary<string, int>` fails with CS9215 because its `Add` takes two arguments.
- Can a method keep the ReadOnlySpan<int> it receives through params for later use?No. `ReadOnlySpan<T>` is a ref struct, so a class field of that type is CS8345, and an `async` method cannot even declare the parameter (CS4012). The span points into the caller's stack frame, which is gone after the call. Copy the values with `values.ToArray()` or choose `params IReadOnlyList<int>` instead.
- params still accepts only single-dimensional arrays in current C#.
- params ReadOnlySpan<T> arguments are first copied into a heap array.
- params IEnumerable<T> receives a lazy iterator over the arguments.
- Passing an existing List<T> to params List<T> wraps it in a new list.
- Any type that implements IEnumerable<T>, Stack<T> included, works with params.
Assumes C# 13 or later for params collections and .NET 9 or later for the BCL's params ReadOnlySpan<T> overloads; checked with C# 14 on .NET 10.