Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Variable Arguments (Varargs) in C#
The params keyword in C# allows a method to accept a variable number of arguments of the same type. This feature, known as variable arguments or varargs, enables you to call a method with any number of parameters without creating an array explicitly.
Syntax
Following is the syntax for declaring a method with variable arguments −
public static returnType MethodName(params type[] parameterName) {
// method body
}
The params keyword must be the last parameter in the method signature, and only one params parameter is allowed per method.
Key Rules for params
-
The
paramsparameter must be the last parameter in the method signature. -
Only one params parameter is allowed per method.
-
You can pass zero or more arguments of the specified type.
-
Arguments are automatically converted to an array inside the method.
Using params for Variable Arguments
Example
Let us see an example to multiply integers using params keyword −
using System;
class Program {
static void Main() {
int mulVal1 = Multiply(5);
int mulVal2 = Multiply(5, 10);
int mulVal3 = Multiply(2, 3, 4);
Console.WriteLine("Multiply(5): " + mulVal1);
Console.WriteLine("Multiply(5, 10): " + mulVal2);
Console.WriteLine("Multiply(2, 3, 4): " + mulVal3);
}
static int Multiply(params int[] numbers) {
int result = 1;
foreach (int num in numbers) {
result = result * num;
}
return result;
}
}
The output of the above code is −
Multiply(5): 5 Multiply(5, 10): 50 Multiply(2, 3, 4): 24
Using params with String Values
Example
using System;
class Program {
static void Main() {
DisplayNames("Alice");
DisplayNames("Bob", "Charlie");
DisplayNames("David", "Emma", "Frank", "Grace");
}
static void DisplayNames(params string[] names) {
Console.Write("Names: ");
for (int i = 0; i < names.Length; i++) {
Console.Write(names[i]);
if (i < names.Length - 1) {
Console.Write(", ");
}
}
Console.WriteLine();
}
}
The output of the above code is −
Names: Alice Names: Bob, Charlie Names: David, Emma, Frank, Grace
Combining Regular Parameters with params
Example
using System;
class Program {
static void Main() {
Calculate("Addition", 10, 20, 30);
Calculate("Multiplication", 2, 3, 4, 5);
}
static void Calculate(string operation, params int[] numbers) {
int result = numbers[0];
Console.Write(operation + " of ");
for (int i = 0; i < numbers.Length; i++) {
Console.Write(numbers[i]);
if (i < numbers.Length - 1) Console.Write(", ");
}
if (operation == "Addition") {
result = 0;
foreach (int num in numbers) {
result += num;
}
} else if (operation == "Multiplication") {
result = 1;
foreach (int num in numbers) {
result *= num;
}
}
Console.WriteLine(" = " + result);
}
}
The output of the above code is −
Addition of 10, 20, 30 = 60 Multiplication of 2, 3, 4, 5 = 120
Conclusion
The params keyword in C# provides flexibility by allowing methods to accept a variable number of arguments of the same type. It must be the last parameter in the method signature and automatically converts the passed arguments into an array for processing within the method.
