By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
The params keyword in C# allows you to pass a variable number of arguments to a method. This feature is crucial for writing flexible and reusable code. It's especially useful in scenarios where the number of inputs can vary, such as logging methods, mathematical functions, or any method that needs to handle an arbitrary number of parameters. Getting this wrong can lead to rigid, inflexible code that's hard to maintain and extend. For example, a logging method that can't handle variable inputs might require multiple overloads, making the codebase harder to manage.
csharp public void LogMessages(params string[] messages) { foreach (var message in messages) { Console.WriteLine(message); } }
⚠️ Common pitfall: Forgetting to use the params keyword can lead to compilation errors.
Call the method with variable arguments:
csharp LogMessages("Error occurred", "Check the logs", "Retry the operation");
⚠️ Common pitfall: Passing arguments of the wrong type can cause runtime errors.
Handle the array inside the method:
csharp public int Sum(params int[] numbers) { int total = 0; foreach (var number in numbers) { total += number; } return total; }
Experts view the params keyword as a tool for writing adaptable and maintainable code. They focus on designing methods that can handle a wide range of inputs without sacrificing clarity or performance. Instead of creating multiple overloads, they use params to simplify method signatures and reduce code duplication.
Exam trap: Questions that involve multi-dimensional arrays with params.
The mistake: Placing params before other parameters.
Exam trap: Method signatures with params in the wrong position.
The mistake: Not handling null or empty arrays.
Exam trap: Scenarios where the method is called with no arguments.
The mistake: Passing arguments of the wrong type.
Scenario 1: You need to create a method that calculates the average of a variable number of integers.Question: Write the method and demonstrate its usage.Solution:
public double CalculateAverage(params int[] numbers) { if (numbers == null || numbers.Length == 0) { return 0; } int total = 0; foreach (var number in numbers) { total += number; } return (double)total / numbers.Length; }
Answer:
double average = CalculateAverage(10, 20, 30, 40);
Why it works: The method uses params to accept any number of integers and calculates the average by summing the numbers and dividing by the count.
Scenario 2: You need to log a variable number of messages to the console.Question: Write the method and demonstrate its usage.Solution:
public void LogMessages(params string[] messages) { foreach (var message in messages) { Console.WriteLine(message); } }
LogMessages("Error occurred", "Check the logs", "Retry the operation");
Why it works: The method uses params to accept any number of string messages and prints each message to the console.
params Type[] parameterName
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.