Reducing overloading ceremony with C# 4.0 optional parameters
It could be because I’m still suffering from upgrade fatigue due to a massive migration to .NET 3.5 that we recently completed at work.
Then again, it might just be because C# 4.0 has received little fanfare compared to the LINQ and lambda magic unveiled in C# 3.0.
Whatever the reason, I didn’t get around to installing Visual Studio 2010 and experimenting with the new C# 4.0 features until just a few days ago.
Optional and Named Parameters, the first feature that I’m excited about, appears like a pretty minor feature on the surface but it has the potential to eliminate a lot of noisy “ceremony code”.
The concept is very simple and familiar to anyone who has specified a default value in a SQL Server stored procedure.
Up until now, when we’ve wanted to provide defaults for a method in C# and offer the API consumer a more concise way to call the method, we’ve had to rely on overloading.
Take the following common case code snippet as an example:
1: public void TakeOrder(string name)
2: {
3: this.TakeOrder(name, "Americano");
4: }
5:
6: public void TakeOrder(string name, string drink)
7: {
8: this.TakeOrder(name, drink, "grande");
9: }
10:
11: public void TakeOrder(string name, string drink, string size)
12: {
13: string order = String.Format("{0} wants a {1} {2}", name, size, drink);
14: orders.Add(order);
15: }
Although the client code below is nice and concise, the method definitions contain a lot of duplication.
1: StarbuxWench wenchDuJour = new StarbuxWench("Russ");
2: wenchDuJour.TakeOrder("Brian");
3: wenchDuJour.TakeOrder("Grif", "Mocha");
4: wenchDuJour.TakeOrder("Kim", "Latte", "tall");
6: wenchDuJour.PlaceOrder();
You might not notice it at first because you are used to it, but imagine if you had to provide a separate sproc definition for each optional parameter in a stored procedure.
Besides being noisy and high on “ceremony”, overloading is suboptimal because it doesn’t make the default values that are being utilized explicit, which can sometimes lead to unpleasant surprises for the consumer. I recently experienced this first hand when I wrongly assumed that the default value for comparison type for String.Compare was a case insensitive comparison.
Another limitation of overloading is that it requires a unique combination of types, which means that I couldn’t provide an overload with just name and size because they are both strings and I already have a definition with two strings (name and drink).
Now consider the C# 4.0 alternative:
1: public void TakeOrder(string name, string drink = "Americano", string size = "grande")
2: {
3: string order = String.Format("{0} wants a {1} {2}", name, size, drink);
4: orders.Add(order);
5: }
Besides being much more concise and saving you the trouble of having to modify your code every time you want to expose a new parameter combination, it also solves the unique type combination limitation as well as hiding default parameteres from the client.
Here you can see that I’m skipping the second parameter by using the new named parameter syntax, which should be very familiar if you’ve ever worked with ruby.
1: wenchDuJour.TakeOrder("Jacob", size: "grande");
Here’s a snapshot of the intellisense, which makes the defaults explicit.
![]()
As I mentioned before, this feature is pretty trivial when compared with lambdas or LINQ, but anything that removes cruft from my code is a welcome addition to the language.
Popularity: 1% [?]
Comments(1)
an application is running as 32-bit is to look for the *32 suffix in task manager. When testing a pre-upgraded version of the app, the suffix was there. After the upgrade it wasn’t there.
A friend of mine recently revealed a super, ninja-level consultant secret to me.
I’m deeply sorry.
It’s been a few weeks since I’ve taken the
As part of the 

