The params keyword lets you send any number of values to a method without making many overloads. It makes your code simpler and easier to use.
When you want a method to accept a flexible number of inputs, like adding many numbers.
When you want to pass a list of items without creating an array first.
When you want to write a method that can handle zero or more arguments easily.
When you want to collect multiple values into one parameter for processing.
Syntax
C Sharp (C#)
void MethodName(params Type[] parameterName) {
// method body
}
The params parameter must be the last in the method's parameter list.
You can pass zero or more arguments, and they will be treated as an array inside the method.
Examples
This method prints any number of integers passed to it.
C Sharp (C#)
void PrintNumbers(params int[] numbers) {
foreach (int num in numbers) {
Console.WriteLine(num);
}
}
Calling the method with four numbers directly.
C Sharp (C#)
PrintNumbers(1, 2, 3, 4);
Calling the method with no arguments is allowed.
C Sharp (C#)
PrintNumbers();
You can also pass an array directly.
C Sharp (C#)
int[] arr = {5, 6, 7};
PrintNumbers(arr);
Sample Program
This program shows how to use params to print any number of messages. It works with many or no messages.
C Sharp (C#)
using System;
class Program {
staticvoid PrintMessages(params string[] messages) {
Console.WriteLine("You sent " + messages.Length + " messages:");
foreach (string msg in messages) {
Console.WriteLine("- " + msg);
}
}
staticvoid Main() {
PrintMessages("Hello", "How are you?", "Goodbye!");
PrintMessages();
}
}
OutputSuccess
Important Notes
You can only have one params parameter in a method.
The params keyword helps avoid creating many method versions for different argument counts.
Summary
Params lets methods accept flexible numbers of arguments.
It must be the last parameter and is treated as an array inside the method.
It makes calling methods easier and your code cleaner.
Practice
(1/5)
1. What does the params keyword do in a C# method?
easy
A. Makes the method run faster by optimizing loops.
B. Allows the method to accept a variable number of arguments as an array.
C. Declares a method as asynchronous.
D. Specifies that the method returns multiple values.
Solution
Step 1: Understand the purpose of params
The params keyword 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, which params does not do.
Final Answer:
Allows the method to accept a variable number of arguments as an array. -> Option B
Quick Check:
params = variable arguments [OK]
Hint: Remember: params means flexible number of inputs [OK]
Common Mistakes:
Thinking params changes method speed
Confusing params with async keyword
Believing params allows multiple return values
2. Which of the following is the correct syntax to declare a method using params in C#?
easy
A. public void PrintNumbers(params int numbers[]) { }
B. public void PrintNumbers(int params[] numbers) { }
C. public void PrintNumbers(int[] params numbers) { }
D. public void PrintNumbers(params int[] numbers) { }
Solution
Step 1: Recall correct params syntax
The params keyword comes before the array type and parameter name: params int[] numbers.
Step 2: Identify incorrect placements
Options B, C, and D place params incorrectly or use invalid syntax for arrays in C#.
Final Answer:
public void PrintNumbers(params int[] numbers) { } -> Option D
Quick Check:
params before array type [OK]
Hint: Put params right before the array type [OK]
Common Mistakes:
Placing params after the type or variable name
Using square brackets incorrectly
Confusing params with pointer syntax
3. What is the output of this C# code?
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", "!");
}
}
medium
A. hello world !
B. hello world!
C. hello, world, !
D. Compilation error
Solution
Step 1: Understand method call with params
The method PrintAll accepts 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 A
Quick Check:
Params prints each argument with space [OK]
Hint: Params passes arguments as array, loop prints each [OK]
Common Mistakes:
Missing spaces between words
Expecting commas instead of spaces
Thinking it causes compile errors
4. Identify the error in this method declaration:
public void Show(params int[] numbers, string label) {
Console.WriteLine(label);
}
medium
A. The 'params' parameter must be the last parameter in the method.
B. The 'params' keyword cannot be used with arrays.
C. The method must return a value when using 'params'.
D. The 'params' keyword requires the parameter to be optional.
Solution
Step 1: Check parameter order rules for params
The params parameter must be the last one in the method signature.
Step 2: Identify the error
Here, params int[] numbers is before string label, which is invalid.
Final Answer:
The 'params' parameter must be the last parameter in the method. -> Option A
Quick Check:
params must be last [OK]
Hint: Params must always be last parameter [OK]
Common Mistakes:
Placing params before other parameters
Thinking params can be anywhere
Confusing params with optional parameters
5. You want to create a method SumAll that sums any number of integers passed to it. Which of these method declarations correctly uses params and returns the sum?
hard
A. public int SumAll(params int numbers) { return numbers.Length; }
B. public int SumAll(int[] numbers, params int n) { return numbers.Sum() + n; }
C. public int SumAll(params int[] numbers) { int sum = 0; foreach(var n in numbers) sum += n; return sum; }
D. public int SumAll(params int[] numbers) { return numbers[0]; }
Solution
Step 1: Check correct use of params and summing logic
public int SumAll(params int[] numbers) { int sum = 0; foreach(var n in numbers) sum += n; return sum; } correctly declares params int[] numbers and sums all elements in a loop.
Step 2: Evaluate other options
public int SumAll(int[] numbers, params int n) { return numbers.Sum() + n; } has invalid params int n after array; public int SumAll(params int numbers) { return numbers.Length; } uses params int numbers which 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 C
Quick Check:
Params array summed with loop [OK]
Hint: Params must be array; sum with loop or LINQ [OK]
Common Mistakes:
Using params with single int instead of array
Placing params parameter incorrectly
Returning only first element instead of sum
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.