#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:

String.Format:

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);

Console.WriteLine:

            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).

Path.Combine:

// 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
    http://whatiseeinit.blogspot.com/

    • 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.
      –Sean

Leave a comment