Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The params keyword is a parameter modifier in C# that enables a method to accept a variable number of arguments of a specified type. When invoked, the compiler automatically aggregates the provided comma-separated values into a single-dimensional array (or, starting in C# 13, other supported collection types) and passes it to the method’s execution context.
// Method signature using the params modifier
public void ProcessData(int requiredArg, params string[] variableArgs)
{
    // variableArgs is treated as a standard string[] within the method body
    int length = variableArgs.Length; 
}

Invocation Mechanics

The compiler handles params arguments by dynamically generating the underlying collection at the call site. You can invoke a params method in three distinct ways:
// 1. Passing discrete, comma-separated arguments
ProcessData(100, "Alpha", "Beta", "Gamma");
// Compiler translates to: ProcessData(100, new string[] { "Alpha", "Beta", "Gamma" });

// 2. Passing zero arguments for the params parameter
ProcessData(200);
// Compiler translates to: ProcessData(200, Array.Empty<string>());

// 3. Passing an explicitly instantiated array
string[] existingArray = new string[] { "Delta", "Epsilon" };
ProcessData(300, existingArray);
// Compiler passes the reference to existingArray directly.

Architectural Rules and Constraints

  • Positioning: The params parameter must be the absolute last parameter in the formal parameter list. Signatures like void Method(params int[] numbers, string text) will result in a compiler error (CS0231).
  • Multiplicity: A method signature can declare a maximum of one params parameter.
  • Type Requirements: Prior to C# 13, the params type was strictly limited to single-dimensional arrays (e.g., int[], object[]). C# 13 introduced params collections, which builds upon the C# 12 collection expression infrastructure to allow params to be applied to types like Span<T>, ReadOnlySpan<T>, List<T>, and IEnumerable<T>.
  • Mutually Exclusive Modifiers: A params parameter cannot be combined with in, ref, or out modifiers.
  • No Default Values: A params parameter cannot be assigned a default value in the method signature. Declarations such as params string[] args = null will result in a compiler error.
  • Nullability: If the caller omits the params arguments entirely, the compiler injects an empty array (or empty span/collection), guaranteeing the parameter is not null. However, a caller can explicitly pass null (e.g., ProcessData(100, null)), which will result in a null reference inside the method.
  • Overload Resolution Precedence: During method binding, the C# compiler prioritizes exact signature matches over params expansions. If Method(int, string) and Method(int, params string[]) both exist, calling Method(1, "A") will bind to the exact match, bypassing the array allocation overhead of the params overload.
Master C# with Deep Grasping Methodology!Learn More