#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");
Advertisements