Visual C# 4.0 Optional Arguments - FINALLY

There is a set of new language features in the upcoming C# 4.0 release. This is probably my favorite new feature. Not only does it warrant a post in it’s own right. It also gives me a chance to point out the Swedish Visual Studio 2010 and .NET Framework 4 launch site and point to all the other great bloggers on that site. Most of them are personal friends of mine and all are worth reading.

So… Optional Arguments in C# 4.0. First an anecdote.

I was sitting in the audience of the PACKED and overflowed (and and re-run) session on C"# 4.0 language features featuring the one and only Anders Hejlsberg at the PDC last year. Next to me were two Microsoft Office developers. When he announced the addition of optional arguments (and named arguments = a later post) I thought I saw the gleam of a tear in the eyes of one of those guys. Well… not really I am embellishing. However they were very exited and jumped up and down on the seats exclaiming how long they have suffered before this addition to C#.

And just to add another anecdote and not at all come to the point of this post. ;~) I saw a video from the C# meeting room a while back, I think it was C# 4.0 meet the design team. Anyway Anders explained some of his personal scrutiny process for suggested language features for C#. It basically goes like this. He listens to the suggestion. Then he says to the one making the suggestion; well it’s like this – you start with –500 “points” of developer value for this feature. Now explain to me how this feature is good enough to get to the surface. (Or something like that.)

Point is – Optional Arguments CERTAINLY is such a language feature and more.

Gone are all those pesky overloads for adding just one more parameter to a constructor or method – gone I tell you - GONE! Talk about good riddance!

A old nasty constructor overload mess like this:

public Person(int shoesize)
{ }

public Person(int shoesize, string name)
{ }

public Person(int shoesize, string name, Gender gender)
{ }

Now becomes this:

public Person(int shoesize, string name = "Eve", Gender gender = Gender.Female)
{   }

Notice how this last constructor has one non-optional (regular) parameter shoesize and two optionals which are given defaults if they are omitted during design time. Optional parameters must be specified at the end of the parameter list.

If you want to specify omit one of the optional arguments you can just leave that argument out in your call to the method or constructor like this.

new Person(12, , Gender.Male);

This would cerate a new instance of a Person that would be a man named Eve with size 12 shoes. (nasty thought but great code.) Of course this becomes much nicer when you combine Optional Arguments with Named Arguments. More on this later.

Here is the reason why COM and Office programmers are nearly weeping for this new language feature (cut from the C# Programming Guide):

var excelApp = new Microsoft.Office.Interop.Excel.Application();
var myFormat = Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatAccounting1;
excelApp.get_Range("A1", "B4").AutoFormat(myFormat, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Now becomes:

var excelApp = new Microsoft.Office.Interop.Excel.Application();
var myFormat = Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatAccounting1;
excelApp.get_Range("A1", "B4").AutoFormat(Format: myFormat);

No more Type.Missing!

Seriously this will clean up a lot of code sooo much. I’ve even found myself at times designing code different because I want to skip overload messiness. Not it’s GONE!

Cheers,

M.

P.S. The coolest new language feature in C# 4.0 is by far the new statically typed Dynamic type. I’ll post on that soon. ;~)

Technorati Tags: ,
Digg This

posted @ Monday, October 05, 2009 12:00 PM

Print

Comments on this entry:

# re: Visual C# 4.0 Optional Arguments - FINALLY

Left by Mattias Sjögren at 10/6/2009 11:14 AM
Gravatar
But the main reason why optional arguments weren't added until C# 4.0 still stands - versioning! Once you've specified the default value for a parameter, it gets embedded at the call site and changing it will give you versioning problems. Plus, I think they are still not considered CLS compliant. So while it may be a convenient feature when consuming Office COM APIs, I would think twice before exposing methods with optional parameters in my own libraries.

# re: Visual C# 4.0 Optional Arguments - FINALLY

Left by Scott Bellware at 10/6/2009 1:34 PM
Gravatar
The risk with the optional features is the same risk as the "const" keyword: the compiler in-lines the values.

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 3 and type the answer here:
 

Live Comment Preview: