#513 – Some Familiar Methods that Accept Parameter Arrays

There are a number of methods in classes in the .NET Framework that accept parameter arrays, allowing you to pass a variable number of arguments to them.  Here are some examples:


int ft = 5;
int inches = 2;
string eyeColor = "blue";
string exclamation = "oh";
// string string.Format(string format, params object[] args)
string lyric = string.Format("{0} ft {1}, eyes of {2}; but {3} what those {0} foot could do", ft, inches, eyeColor, exclamation);


            string[] favs = {"Kittens", "Mittens", "Packages", "Tractors", "Streudel"};
            // void Console.WriteLine(string format, params object[] args)
            Console.WriteLine("Fav things: {0}, {1}, {2}, {3}, {4}", favs);

(Note: Console.WriteLine has an overload that accepts four object parameters after the format string, so the version with the parameter array doesn’t kick in until you use at least five parameters).


// string Path.Combine(params string[] paths)
string path = Path.Combine("D:\\", "Sean", "Notes", "WCF");

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #513 – Some Familiar Methods that Accept Parameter Arrays

  1. Hi Sean,
    You should point out that the last point is only true for .Net 4 and higher.

    Reuben Bartolo

    • Sean says:

      Thanks Reuben, yes true–and good to know if you’re working with 3.5 or earlier. In general, though, I don’t get into documenting differences between the different versions of the framework. Instead, I try to make sure that my posts accurately reflect the current version. That way, things hold true for people writing code against the latest/greatest version–which is probably true for most people. Thanks for pointing out the difference, though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: