ISerialized .Net, C#, Scrum and agile software development

9Apr/102

Optional parameters in C# 4.0

A while back I blogged about the great features of the Dynamic Language Runtime introduced in .Net 4.0. Today I will look into another great feature introduced in C# 4.0, namely the new  named and optional parameters.

If you are familiar with C++, you have probably used optional parameters earlier, but C# has missed this feature until now. As a work around we had to use overloaded methods, but in the concept of clean code, I'd rather prefer to use optional parameters!

There are two new features in C# 4.0 on this:

  • Named parameters
  • Optional parameters

Both features are very easy to understand, and even easier to use, so this post will just include a couple of simple examples just to show what it is and how easy it is to use them!

Named parameters

The named parameters feature allows you to add parameters to a method (or constructor) in any order you like,  and hence no need to remember the order of the parameters of the method, and also (in my point of view) makes it easier to read you code!

Given the follwoing method:

private void MyMethod(int length, int height, string title)
{
    //Do some work
}

Using named parameters, I can add the parameters in any order I want like this:

MyMethod(title: "mytest", length: 123, height: 554);

However, there are only two things to notice here:

  • A named parameter can follow a positional parameter (eg, like MyMethod(123, title: "mytest", height: 554)
  • A positional parameter can not follow a named parameter

The strength of named parameters is shown when it's later combined with optional parameters!

Optional parameters

Ever since I switched from C++ to C# development, I have missed the optional parameter feature! Take a look at the following familiar methods:

private void MyMethod(int length, int height, string title)
{
    //Do some work
}

private void MyMethod(int length, string title)
{
    MyMethod(length,1,title);
}

private void MyMethod(int length, int height)
{
    MyMethod(lenght, height, "");
}

Ever seen such a setup before? The two last methods here, do not contain anything interesting at all what so ever, other than allowing us to call the first MyMethod with a set a default parameters. Now I can however do the following with optional parameters:

private void MyMethod(int length, int height = 1, string title = "")
{
    //Do some work
}

The code becomes much easier to read, and we can easily see which parameters are optional.  We can call this method with any parameters we like using named parameter, as long as the non-optional parameter is present

MyMethod(200);
MyMethod(200, title: "My own title");

Features like lamda expressions, Linq and extension methods are very nice features that came in .Net earlier, but requires the developers to use them for a while to really see the full benefit of them. Named and optional parameters on the other hand, are easy to understand and will probably rapidly increase in popularity.

What puzzles is that this feature has not been available earlier.... 

Posted by Pål Eie

Comments (2) Trackbacks (0)
  1. Cool stuff! I actually understood most of this :)
    Thanks Paul!

  2. :) Thats good!

    I guess that means you move forward in your quest the .Net glory! ;)


Leave a comment

No trackbacks yet.