CHAPTER 4 CORE C# PROGRAMMING CONSTRUCTS, PART II
Invoking Methods Using Named Parameters
Another language feature found in C# is support for named arguments. To be honest, at first glance, this
language construct might appear to do little more than result in confusing code. And to continue being
completely honest, this could be the case! Similar to optional arguments, including support for named
parameters is largely motivated by the desire to simplify the process of working with the COM
interoperability layer (again, see Chapter 16).
Named arguments allow you to invoke a method by specifying parameter values in any order you
choose. Thus, rather than passing parameters solely by position (as you will do in most cases), you can
choose to specify each argument by name using a colon operator. To illustrate the use of named
arguments, assume we have added the following method to the Program class:
static void DisplayFancyMessage(ConsoleColor textColor,
ConsoleColor backgroundColor, string message)
{
// Store old colors to restore after message is printed.
ConsoleColor oldTextColor = Console.ForegroundColor;
ConsoleColor oldbackgroundColor = Console.BackgroundColor;
// Set new colors and print message.
Console.ForegroundColor = textColor;
Console.BackgroundColor = backgroundColor;
Console.WriteLine(message);
// Restore previous colors.
Console.ForegroundColor = oldTextColor;
Console.BackgroundColor = oldbackgroundColor;
}
Now, the way DisplayFancyMessage() was written, you would expect the caller to invoke this method
by passing two ConsoleColor variables followed by a string type. However, using named arguments, the
following calls are completely fine:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Methods *****");
...
DisplayFancyMessage(message: "Wow! Very Fancy indeed!",
textColor: ConsoleColor.DarkRed,
backgroundColor: ConsoleColor.White);
DisplayFancyMessage(backgroundColor: ConsoleColor.Green,
message: "Testing...",
textColor: ConsoleColor.DarkBlue);
Console.ReadLine();
}
One minor gotcha regarding named arguments is that if you begin to invoke a method using
positional parameters, they must be listed before any named parameters. In other words, named
arguments must always be packed onto the end of a method call. The following code is an example:
129