Parameters: oh, I don’t know how many I need…

Published Fri, Sep 3 2010 11:37

If you’re a long time programmer/developer, then you probably expect to be able to create a parameter that receives a variable number of parameters.In C#, to declare a method that accepts a variable number of parameters you need to qualify the parameter with the params keyword. Here’s a quick example:

static void PrintNames( params String[] names){
    foreach (var name in names) {
        Console.WriteLine(name);
    }
}

As you can see, you use an array to represent a variable number of parameters. Notice that you can only apply the params keyword to the last parameter defined by a method and that parameter’s type must be an array of a single dimension (of any type). When you add the params qualifier, you’re essentially simplifying the invocation code:

PrintNames("Luis", "Miguel", "Nunes", "Abreu");

As you can see from the previous snippet, you don’t need to pass an array to the PrintNames method. If you want, you can create an array and call the method, but I’m thinking that most people will prefer the approach used in the previous snippet.

When the compiler finds the params keyword, it applies the ParamArrayAttribute to that parameter. Whenever the C# compiler detects a call to a method, it will first try to match that calls against a method which hasn’t any parameter annotated with the ParamArrayAttribute.  It will only consider methods with a variable number of parameters (ie, that use the params keyword) when it can’t find a match in the previous candidate list.

Before ending, a performance related note: calling a method with a variable number of parameters incurs in a small performance hit since the compiler needs to transform the list of values into an array (in other words, the method will always receive an array, even though you haven’t built one explicitly). btw, this does not happen if you pass null to the method. And as you’ve seen, it takes longer to find that method because the compiler will first try to find a method which doesn’t use a variable number of parameters. That’s why you’ll find several classes which define several overloads with a variable number of parameters before introducing the method which has a single parameter annotated with the params qualifier (ex.: String’s Concat method).

And that’s it for now. Stay tuned for more.

Filed under: ,

Comments

# Jef Claes said on Friday, September 03, 2010 7:29 AM

So the performance hit is at compile time and not at runtime?

# luisabreu said on Friday, September 03, 2010 8:56 AM

You can say that you'll have both (I'd also say they're negligible). at compile time, the compiler will have to generate the correct code for making the call (it will take a little longer) and at runtime you'll have the performance hit that results from the array creation. Btw, I've only mentioned this as a side note. It shouldn't really matter that much unless you're passing lots of parameters, right?

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover