Unknown C# keywords: params

Often overlooked, and (some may say) unloved, is the params keyword, but it’s an awesome keyword, and one you should definitely check out.

What does it do?

Well, it lets you specify a single parameter that can have a variable number of arguments.

You what?

Best shown with an example, let’s say we write an add method:

public int Add(int first, int second)
{
    return first + second;
}

meh, it’s alright, does what it says on the tin, but it’s not exactly awe-inspiring…

Oh noes! You need to add 3 things together???

public int Add(int first, int second, int third)
{
    return first + second + third;
}

oh yes, you code master you! Overloading a-plenty!

Now a fourth…

Ok, this is starting to get a bit ridiculous, if only there was some way…

public int Add(int first, int second, params int[] others)
{
   return first + second + others.Sum();
}

So now I can call this with any number of int arguments? – well, any number > 2..?

Yes!

int ret = Add(1, 2, 3);

Is as valid as:

int ret = Add(1, 2, 3, 4);

Of course you probably won’t really need to ever do that method, so what could you use it for? How about adding strings together? What about a logging method?

We all know ToString can be an expensive method to call, it might traverse every node on a class hierarchy, or may just be the name of the type… either way, we don’t really want to call it if we can avoid it, so how about a logging method like so:

public void Log(LogLevel level, params object[] objs)
{
     if(LoggingLevel < level)
        return;

    StringBuilder output = new StringBuilder();
    foreach(var obj in objs)
        output.Append((obj == null) ? "null" : obj.ToString());

    return output;
}

Now we only call ‘ToString’ when we want to, and because we’re passing in just objects we don’t have to call ToString if the Logging Level isn’t what we want…

Of course, there are a multitude of things to use params for…

Print | posted @ Tuesday, February 1, 2011 3:47 AM

Comments on this entry:

Gravatar # re: Unknown C# keywords: params
by David Robbins at 2/2/2011 2:06 AM

Nice post. In a sense params is a substitute for Tuples.

I have adopted a different strategy - I use Dictionary<string, object> as a parameters object. This way I can avoid getting the order of parameters wrong when call the mehthod.
Gravatar # re: Unknown C# keywords: params
by naraga at 2/2/2011 4:36 AM

have you considered to use dedicated class/struct instead of dictionary david?
Gravatar # re: Unknown C# keywords: params
by Chris at 2/2/2011 6:08 AM

I guess it comes down to how you want to do your params, I assume that the order I get the values is the order I want to display them in.

I guess you could use:

params Tuple[] tuples

or even params KeyValuePair[] kvPairs

if you wanted to go with a dictionary stylee :)
Gravatar # re: Unknown C# keywords: params
by Ryan at 2/3/2011 3:21 AM

@David, @naraga Resharper has an "extract parameters to class" refactoring that can become very useful when cleaning up methods with lots of parameters.
Post A Comment
Title:
Name:
Email:
Comment:
Verification: