Params keyword for variable arguments in C Sharp (C#) - Time & Space Complexity
When a method accepts a variable number of arguments using the params keyword, it can handle many inputs flexibly.
We want to understand how the time to run the method changes as we give it more arguments.
Analyze the time complexity of the following code snippet.
public void PrintNumbers(params int[] numbers)
{
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
This method prints each number passed to it, no matter how many numbers are given.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the
numbersarray. - How many times: Once for each number passed to the method.
As you give more numbers, the method prints more lines, doing more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print actions |
| 100 | 100 print actions |
| 1000 | 1000 print actions |
Pattern observation: The work grows directly with the number of inputs; double the inputs, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of arguments given.
[X] Wrong: "Using params makes the method run instantly no matter how many arguments."
[OK] Correct: The method still processes each argument one by one, so more arguments mean more work and more time.
Understanding how variable arguments affect performance shows you can think about how code scales, a useful skill in real projects and interviews.
"What if the method used recursion instead of a loop to process the params array? How would the time complexity change?"
Practice
params keyword do in a C# method?Solution
Step 1: Understand the purpose of
Theparamsparamskeyword allows a method to accept zero or more arguments as a single array parameter.Step 2: Compare with other options
Options B, C, and D describe unrelated features: performance, async methods, and multiple return values, whichparamsdoes not do.Final Answer:
Allows the method to accept a variable number of arguments as an array. -> Option BQuick Check:
params= variable arguments [OK]
- Thinking params changes method speed
- Confusing params with async keyword
- Believing params allows multiple return values
params in C#?Solution
Step 1: Recall correct
Theparamssyntaxparamskeyword comes before the array type and parameter name:params int[] numbers.Step 2: Identify incorrect placements
Options B, C, and D placeparamsincorrectly or use invalid syntax for arrays in C#.Final Answer:
public void PrintNumbers(params int[] numbers) { } -> Option DQuick Check:
paramsbefore array type [OK]
- Placing params after the type or variable name
- Using square brackets incorrectly
- Confusing params with pointer syntax
public class Program {
public static void PrintAll(params string[] words) {
foreach (var word in words) {
Console.Write(word + " ");
}
}
public static void Main() {
PrintAll("hello", "world", "!");
}
}Solution
Step 1: Understand method call with params
The methodPrintAllaccepts any number of strings as an array and prints each with a space after it.Step 2: Trace the output
It prints "hello ", then "world ", then "! ", resulting in "hello world ! " with spaces after each word.Final Answer:
hello world ! -> Option AQuick Check:
Params prints each argument with space [OK]
- Missing spaces between words
- Expecting commas instead of spaces
- Thinking it causes compile errors
public void Show(params int[] numbers, string label) {
Console.WriteLine(label);
}Solution
Step 1: Check parameter order rules for
Theparamsparamsparameter must be the last one in the method signature.Step 2: Identify the error
Here,params int[] numbersis beforestring label, which is invalid.Final Answer:
The 'params' parameter must be the last parameter in the method. -> Option AQuick Check:
paramsmust be last [OK]
- Placing params before other parameters
- Thinking params can be anywhere
- Confusing params with optional parameters
SumAll that sums any number of integers passed to it. Which of these method declarations correctly uses params and returns the sum?Solution
Step 1: Check correct use of
public int SumAll(params int[] numbers) { int sum = 0; foreach(var n in numbers) sum += n; return sum; } correctly declaresparamsand summing logicparams int[] numbersand sums all elements in a loop.Step 2: Evaluate other options
public int SumAll(int[] numbers, params int n) { return numbers.Sum() + n; } has invalidparams int nafter array; public int SumAll(params int numbers) { return numbers.Length; } usesparams int numberswhich is invalid; public int SumAll(params int[] numbers) { return numbers[0]; } returns only first element, not sum.Final Answer:
public int SumAll(params int[] numbers) { int sum = 0; foreach(var n in numbers) sum += n; return sum; } -> Option CQuick Check:
Params array summed with loop [OK]
- Using params with single int instead of array
- Placing params parameter incorrectly
- Returning only first element instead of sum
Ready to prove it?
Pass the test and this pattern moves unknown
