Parameter Arrays

Some methods can benefit from having an unbounded number of arguments passed to them. So far, we’ve seen an example of this in passing, namely the Console.WriteLine method. Because the first argument can be used to provide a so-called format string, which is a string with placeholders for variant portions, the remainder arguments to fill in those gaps depends on the string itself. For example:

Console.WriteLine("{0} + {1} = {2}", a, b, a + b);

The use of format strings comes in handy when dealing with quite a bit of formatting, where string concatenation would become very ugly and unreadable:

Console.WriteLine(a + " + " + b + " = " + (a + b));

In addition to this, it allows for a clean way to localize strings, such as error messages, ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.