C# 4 has added two new features regarding parameters that should help improve developer productivity. Optional and named parameters give you some new ways to conquer old problems.
Optional Parameters
In the past when we wanted to create methods with varying numbers of parameters we would use overloaded methods. Overloading is the process of creating methods with the same name but of varying parameters. For example, say we have a method named GetRandomNumber which has three different method signatures:
static void Main(string[] args)
private static Random random = new Random();
public static int GetRandomNumber(int max)
{
return random.Next(max);
}
public static int GetRandomNumber(int min, int max)
{
return random.Next(min, max);
}
public static int GetRandomNumber()
{
return random.Next();
}
With C# 4, we now can consolidate these into a single method using optional parameters. Optional parameters allow you to declare and initialize a variable from within the method signature. Any parameter that has a default value assigned to it can be excluded when you invoke the method. Therefore we can consolidate our previous code to:
private static Random random = new Random();
public static int GetRandomNumber(int min = -1, int max = -1)
{
if (min == -1 && max != -1)
return random.Next(max);
else if (min != -1 && max != -1)
return random.Next(min, max);
else
return random.Next();
}
The classic scenario for using optional parameters arises when you are interacting with COM interfaces like the Microsoft Office Automation APIs. If you have worked with the Office APIs before, then I am sure you are familiar with the infamous Type.Missing. Before C# 4, we would have to use the following code to format a range of cells with Excel:
var excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add();
excel.Visible = true;
var format = Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1;
excel.get_Range("B10", "B40").AutoFormat(format,
//and now for 6 Type.Missing parameters....yuck
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
This code seems wasteful due to the fact that we have to fill in six of the seven parameters with Type.Missing. In C# 4, the call to the AutoFormat is simplified. We can basically avoid all the repetitive Type.Missing parameters and only specify the first parameters. This reduces the code to:
excel.get_Range("B10", "B40").AutoFormat( format );
Named Parameters
Finally, the other new feature in C# 4.0 is named parameters. Named parameters come in handy in situations when it is easy to forget what order the parameters come in. Going back to my first example I had a random number generator that took a min and a max value. If for some reason I reversed these parameters my methods would either return the wrong results or fail. However, with named parameters we can “bind” the parameters by specifying the parameter name and the value together. Here is an example:
Console.WriteLine( GetRandomNumber( max : 100, min : 1 ) );
Admittedly, none of the features are earth shattering. We had workarounds for these issues before so its not like we are really gaining anything in terms of functionality. However, these new features will save you some typing. After all, these days its all about productivity!