Using params keyword – Methods with variable number of parameters

Have you ever come to a situation when you want to create C# method (or in any other language), but you are in doubt what number of parameters your method needs to have? I suppose you do!

It’s certain that many of You have already used some collections, such as: Dictionary<key,value>, ArrayList, Array, List<T>, HashSet<T>, which will help us collect items and then pass the collection (with the items) to the method, which accepts parameter of type same as the collection we use.

Example:

protected void Page_Load(object sender, EventArgs e)
{
    Dictionary<string, int> myDict = new Dictionary<string, int>();
    myDict.Add("first", 1);
    myDict.Add("second", 2);
    myDict.Add("third", 3);
    DoThis(myDict);
}

public static void DoThis(Dictionary<string,int> dict)
{
    foreach (var param in dict)
    {
        //play with params
    }
}

But, sometimes we may come to a scenario in which we will need to have one same method, that will accept different number of parameters. One option is to create overloaded method, but, in this blog post I will show you something else.

PARAMS – keyword used before the data type of the parameter we want to send. Using this params keyword, we can create method that will accept variable number of parameters. How to do that?

For illustration, here is an example:

protected void Page_Load(object sender, EventArgs e)
{
    DoThis("hajan1", "hajan2", 1, true, false, "hajan3",
        new object[]{"I", "You", "He", "She", "We"}); //will work
    DoThis(1, 1, 1, 1, 1, 1, 1); //this will also work
    DoThis(); //this will also work :)
}

public static void DoThis(params object[] myParams)
{
    foreach (var param in myParams)
    {
        //play with params
    }
}

As  you can see, the method DoThis() accepts variable number of parameters, thus, it works perfectly and it will not give error message such as: “No overloads for method <MethodName> takes <number> arguments

PARAMS keyword can be also used in combination with another parameters.

Example:

protected void Page_Load(object sender, EventArgs e)
{
    DoThis("Hello, I'm first parameter",
        "I'm probably second, not sure", "I'm third");
}

public static void DoThis(string message, params object[] myParams)
{
    HttpContext.Current.Response.Write(message); //message
    foreach (var param in myParams) //iterate tru params
    {
        //play with params
    }
}

In this case, when we want to use another FIXED parameters (i.e. message), the parameter with keyword params must be used last in the list of parameters of the given method.

Example:

This is impossible.

public static void DoThis(params object[] myParams,string message)
{
    HttpContext.Current.Response.Write(message); //message
    foreach (var param in myParams) //iterate tru params
    {
        //play with params
    }
}

It will automatically give you a error message: “A parameter array must be the last parameter in a formal parameter list”.


NOTE: IN THIS EXAMPLE I’VE USED ‘PARAMS OBJECT[]’ IN ORDER TO BE ABLE TO SEND ANY PARAMETER TYPE, OF COURSE SPECIFYING THE TYPE OF THE PARAMETER IS ALLOWED. Example: params int[] numbers.

The concept of using VARIABLE number of parameters does not exists only in C#, but also in other programming languages too.

In some specific cases, this way might be very useful.

I hope this blog post was useful to you.

Please, do not hesitate to send your feedback & comments.

Best Regards,
Hajan

3 Comments

  • Yep, and it is also worth noting you have optional parameters (with default values) in .NET 4, which can also be combined with the params keyword (as the last param in the method).

    This is nice if you want strongly typed (rather than Object) parameters to be 'optionally' passed, and you can also include 'params object[] theRest' as the last param if you really need too.

    Dave

  • Using params keyword methods with variable number of parameters.. Nice :)

  • thanks alot. A good post!

Comments have been disabled for this content.