Default and Named Parameters in C# 4.0 / Sith Lord in Training

by Joey deVilla on March 12, 2009

Round Trip

sith_lord_in_training Back when I was working for OpenCola (from January 2000 through January 2002), the start-up cofounded by Cory Doctorow, I was doing a lot of work using beta versions of C# to build prototype peer-to-peer applications that got demoed to some large companies, including Microsoft, who were kind enough to provide us with betas of Visual Studio .NET and Windows XP.

I graduated to the 1.0 version when it came out. Even during the year after I left OpenCola (or more accurately, got the boot), I continued to write applications in C#, from things like a sales app for people who were selling practice certification tests to a trivia game for a company that was pitching it to Maxim. I do manage to land some interesting jobs from time to time.

That changed on Bastille Day 2003, my first day as Tucows’ Technical Evangelist, or as the title originally read, “Technical Community Development Coordinator”. Tucows’ client base were people who wanted to resell things like domain names and email, and as such were largely hosting companies. This in turn meant that they were using languages that you might consider “webbier”: open source dynamically-typed languages like Perl, PHP, Python and Ruby. I did what I could to stay away from Perl, I’d coded in PHP and Python for work before, and I picked up Ruby along the way.

Feeling a bit restless, I left Tucows in late 2007 to do Ruby on Rails development at what turned out to be Toronto’s worst-run startup, possibly ever. After that, it was project management at b5media, where I used Ruby to implement some “housekeeping” scripts. Although I hit up Microsoft Evangelist David Crow for a copy of Visual Studio so I could try out XNA, I really didn’t pay too much attention to C#. I installed it on my machine, wrote a lazy “Hello, World” app – a single WinForm with a button that displayed a MessageBox with the word “poop” when you clicked it – and promptly forgot about it.

The situation changed when I got laid off in September and then got hired as a Developer Evangelist for “The Empire” in October. Suddenly, I’m back in a world with a three-versions-later Visual Studio and a two-and-a-bit-versions-later of C# and .NET. I’ve got the programming know-how and the language basics down cold; it’s the changes in the language and library – generics, LINQ and a bunch of 2- and 3-letter acronyms beginning with “W” – that keep catching me by surprise.

Luckily, management is cool with my first year being a “learning journey”. They’re really interested in how I mix my schmoozing and community-building skills with a love of technology and programming and don’t mind that my first year is a “learning journey”. They especially don’t mind if I share what I learn along the way, which is what this series of articles, Sith Lord in Training, is all about. As I learn more about C# and the .NET framework, both present versions and the upcoming 4.0 versions, I’ll write about them here.

Default Parameters in C# 4.0

Suppose that you’ve got a method that takes a single boolean argument. Here’s how the argument affects what the method does":

  • If the argument is anything other than true or if no argument is provided, the method performs its normal task.
  • If the argument is true, the method performs its task, plus some additional stuff.

Here’s the Ruby implementation:

doSomethingOptional is a parameter with a default value. If myMethod is called without any parameters, doSomethingOptional is given the default value of false.

Unfortunately, the current 3.0 version of C# doesn’t support parameter defaults. The way to emulate this behaviour is to use method overloading:

  • One method to handle cases where no parameter is given
  • Another method to handle cases where a parameter is given

Here’s the implementation in C# 3.0:

That’s a bit long-winded for something that should be pretty simple. Luckily, this has been fixed in C# 4.0:

And with that, the long-winded (and unnecessary, at least to my mind) method overloading workaround vanishes. Yay!

Named Parameters in C# 4.0

Named parameters make the meaning of the parameters explicit, as long as the parameter names themselves are pretty meaningful. Contrast the following call:

with this, which is supported in Python:

C# 3.0 doesn’t support named parameters, but C# 4.0 does. Here’s how you’d call MyMethod in C# 4.0 using them:

As for the Python drawCircle method in the example above. here’s how you’d call it in C# 4.0:

If this syntax is giving you some deja vu, it might be because it’s reminding you of Objective-C, where the call would look something like this:

See the Video

If you’d like to see more about default and named parameters in C# 4.0, there’s a video on the Chanel 9 site that covers them quite extensively. Go check it out!

{ 3 comments… read them below or add one }

1 Jörg W Mittag March 13, 2009 at 1:56 pm

Ruby does not support named parameters. What you are simply doing is passing the expression radius = 100 as the first positional argument, and since in Ruby assignment expressions always evaluate to the assigned value, this is exactly the same as passing glorp = 100 or even foo.bar = 100 or just 100. In fact, it is even the same as passing x = 100, which is definitely not what you want from your named parameters!

2 Joey deVilla March 15, 2009 at 6:49 am

Jörg W Mittag: You’re right. I got my Python mixed up with my Ruby!

3 Triynko March 1, 2012 at 4:54 pm

Python supports named parameters, but the syntax sucks, IMO. Like Jörg said, it looks like you’re just passing the result of an assignment expression even though you’re not (Python FAIL 1), because Python doesn’t allow assignment expressions to be used in other expressions (Python FAIL 2). The C# syntax is much clearer “radius:100″, because in terms of natural language, it looks like it’s saying “radius is 100″, whereas with Python’s syntax, in terms of mathematical language, it looks like an assignment expression.

Leave a Comment

{ 1 trackback }

Previous post:

Next post: