S3878 — Arrays should not be created for params parameters

Language
C#
Type
Code smell
Severity
Minor
Tags
clumsy

Why is this an issue?

Creating an array or using a collection expression solely for the purpose of passing it to a params parameter is unnecessary. Simply pass the elements directly, and they will be automatically consolidated into the appropriate collection type.

How to fix it

Code examples

Noncompliant code example


public void Base()
{
    Method(new string[] { "s1", "s2" }); // Noncompliant: resolves to string[] overload
    Method(new string[] { });            // Noncompliant: resolves to string[] overload
    Method(["s3", "s4"]);                // Noncompliant: resolves to ReadOnlySpan overload
    Method(new string[12]);              // Compliant: resolves to string[] overload
}

public void Method(params string[] args)
{
    // ...
}

public void Method(params ReadOnlySpan<string> args) // C# 13 params collections
{
    // C# 13 params collection
}

Compliant solution


public void Base()
{
    Method("s1", "s2");     // resolves to ReadOnlySpan overload
    Method();               // resolves to ReadOnlySpan overload
    Method("s3", "s4");     // resolves to ReadOnlySpan overload
    Method(new string[12]); // resolves to string[]  overload
}

public void Method(params string[] args)
{
    // ..
}

public void Method(params ReadOnlySpan<string> args) // C# 13 params collections
{
    // ..
}

Resources

Documentation

↑ Back to top