Member-only story
C# Params Keyword with Examples
2 min readMay 8, 2024
In C#, “params” is a keyword used in method parameter to indicate that a method can accept a different number of parameters of the same type. This feature is often used when you want to create methods that can accept an undecided number of parameters.
Params in C# with Example One
using System;
public class Program
{
public static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.ExampleMethod(1, 2, 3); // You can pass multiple integers as arguments
program.ExampleMethod(4); // You can pass a single integer
program.ExampleMethod(); // You can even call it with no arguments
}
public void ExampleMethod(params int[] numbers)
{
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
}
Output
1
2
3
4
Params in C# with Example Two
using System;
public class Program
{
public static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.AnotherExampleMethod("Hello", 1, "World", true);
}
public void AnotherExampleMethod(string message, params object[] values)
{
Console.WriteLine(message);
foreach (object val in values)
{
Console.WriteLine(val);
}
}
}