CodeCopy

October 4, 2009

Method with a variable number of parameters

Filed under: c# — mazzoo @ 21:44
Tags: ,

When you need to create a method that handles an undefined number of parameters, one way to define it is by using an Array or list. Like this:

private void Method( string[] arguments )
{
    //...
}

This, however, have a semantic inconvenience when called:

Method( new string[] {  “a”, “b”, “c” };

To overcome this, use params before the string[] identifier, like this

private void Method( params string[] arguments )
{
    //...
}

Now you can call the Method a bit cleaner, making your code more readable:

Method(“a”, ”b”, ”c”);

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.