August 9, 2009 19:14
With the advent of VS2010, there's an explosion of interest around the new features of C#4.
Named parameters.
That's a cool one. Look at the code below and note that without checking with the signature you can't really tell where's the title and where's the message:
public void SendMessage(string title, string message) { ... }
SendMessage("foo", "boo"); //that's how you call it
And look at the C#4 variant - now it's much better, eh?
SendMessage(title: "foo", message: "boo");
The Question.
And now the question that looms in the minds of many, and ardent C# admirers knit their brows in perplexity: why do we have to use ':' for named parameters? Surely the "=" would be much better?
Well, the reason for using colons instead of equals is simple: the equal sign already has a meaning.
int myInt;
CallMe(myInt = 5); //set myInt variable to 5
CallMe(myInt : 5); //set myInt parameter of CallMe method to 5