[Solved] Does C# support a variable number of arguments, and how?

Does C# support a variable number of arguments?

If yes, How does C# support variable no of arguments?

What are the examples?

How are variable arguments useful?

EDIT 1: What are the restrictions on it?

EDIT 2: The Question is not about Optional param But Variable Param

Solution #1:

Yes. The classic example wourld be the params object[] args:

//Allows to pass in any number and types of parameters
public static void Program(params object[] args)

A typical usecase would be passing parameters in a command line environment to a program, where you pass them in as strings. The program has then to validate and assign them correctly.


  • Only one params keyword is permitted per method
  • It has to be the last parameter.

EDIT: After I read your edits, I made mine. The part below also covers methods to achieve variable numbers of arguments, but I think you really were looking for the params way.

Also one of the more classic ones, is called method overloading. You’ve probably used them already a lot:

//both methods have the same name and depending on wether you pass in a parameter
//or not, the first or the second is used.
public static void SayHello() {
public static void SayHello(string message) {

Last but not least the most exiting one: Optional Arguments

//this time we specify a default value for the parameter message
//you now can call both, the method with parameter and the method without.
public static void SayHello(string message = "Hello") {


Solution #2:

C# supports variable length parameter arrays using the params keyword.

Here’s an example.

public static void UseParams(params int[] list)
    for (int i = 0; i < list.Length; i++)
        Console.Write(list[i] + " ");

There’s more info here.

Respondent: hotS85

Solution #3:

Yes, params:

public void SomeMethod(params object[] args)

params has to be the last argument and can be of any type. Not sure if it has to be an array or just an IEnumerable.

Respondent: recursive

Solution #4:

I assume you mean a variable number of method parameters. If so:

void DoSomething(params double[] parms)

(Or mixed with fixed parameters)

void DoSomething(string param1, int param2, params double[] otherParams)


  • They must all be the same type (or of a child type) as is true for arrays as well
  • There can only be one for each method
  • They must come last in the parameter list

That’s all I can think of at the moment though there could be others. Check the documentation for more information.

Respondent: Michael Stum

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *

New ride at Perth's Adventure World a ‘swing on steroids’ - Australasian Leisure Management npp pharma the truth about anabolic steroid drugs and professional sports