Two New Features of C# 4.0

Two new features to C# 4.0 are optional parameters and named parameters. Optional parameters have been a part of VB.Net but it's now possible to do this in C#. Instead of using overload methods, you can do this in C#:

private string SomeMethod(string givenName,
string surname = "Sheridan", int age = 10)
{
return givenName + " " + surname;
}

The second and third parameters, surname & age, both have default values. The only parameter required is givenName. To call this method you can either write this:

string name = null;
name = SomeMethod("Malcolm");

That will return Malcolm Sheridan. You can also do this:

string name = null;
name = SomeMethod("Suprotim", "Agarwal");

The value returned is Suprotim Agarwal. But what if you didn't want to specify a surname but you did want to pass the age? You can do this by using named parameters:

string name = null;
name = SomeMethod("Suprotim", age: 20);

These are nice additions to the language.






About The Author

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET and regular presenter at conferences and user groups throughout Australia. Being an ASP.NET Insider, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with JavaScript. He also blogs regularly at DotNetCurry.com. Follow him on twitter @malcolmsheridan

22 comments:

Anonymous said...

wow! Python had this feature eight years ago.

Anonymous said...

@Anonymous 9:19 AM
And yet, at work they don't allow development in Python, Iron- or no, because unrestricted language choice would lead to more difficulties in code maintenance and future hiring. Can we agree to be happy that C# is stealing from excellent sources?

Anonymous said...

Good, but someone explain how these 2 features help developers and companies? Also why C# has to be case sensitive?

However I love the parallel computing that Microsoft introduces with VS 2010 and .NET 4.0

Anonymous said...

Are there any limitations to this feature? Can you use all objects, not only primitives? Are the default parameter values bound at compile- or run-time?

Bitme said...

Are there any other feature additions made in 4.0?

Malcolm Sheridan said...

@Bitme
Yes there are. I will be posting short articles here about some of the other new features here. Stay tuned.

Anonymous said...

Just wondering ... why do you use the explicit "string name = null" instead of "string name = SomeMethod(...)"? It doesn't really add any security ...

Malcolm Sheridan said...

I just coded it that way for the example. Both ways will work fine.

Sheron said...

Did you really mean to say that your age is just 20..?

Anonymous said...

I hate to break it to you all, but Visual Basic 6 (pre-.NET) had this back in 1998 when it was released. Visual Basic.net has had this the whole time. C# is still doing a little catch up.

It is good to know that python borrowed a nice feature though.

Malcolm Sheridan said...

Yes this feature has been in VB for a while now. It's nice to see C# catching up!

Anonymous said...

Oh yes, was waiting on this feature for years! It so much improves readability its hard to believe it wasn't implemented since C# 1.0

Best regard, Alex (alex.besogonov@gmail.com)

Anonymous said...

Finally VB'ers get their day in court, LOL

Anonymous said...

Excluding the clear benefits when working with COM and other languages, I would be a little cautious about using optional parameters as first choice option compared to overloading.

Consider that when calling a method with optional parameters, all the compiler is doing is filling in the missing parameters which the developer has ommitted using the defaults which have been defined.

Now if that method changes and adds additional parameters, you will need to recompile your callers as well. This could pose issues when (for example) you have actually provided an API and you want to drop a patched DLL. If you do not recompile the callers (you may not know who your callers are), you would have introduced a breaking change.

I would recommend overloading still as a first choice over optional parameters for publicly exposed methods.

Anonymous said...

This is a wonderful information about compiler filling in the optional parameter.

Thanks

Anonymous said...

I wish people would stop this lauguage zealotry. All languages were designed to fit a need. After that they evolve by gaining the best features of existing ones. It is nice to see any language improve. If it was not, it would be dying.

Anonymous said...

I am scared by this feature. Optional parameters are fine, but with named parameters i can no longer rename my parameters without being worried that i will break some code.

Anonymous said...

Very interesting and useful

Matt said...

This looks much like attribute parameters which are already part of .NET. I've kinda missed the optional parameters from VB, but VB.NET is/was also lacking several features that C# supported since day 1.

Anonymous said...

"Just wondering [...] It doesn't really add any security ..."

You'd divide the statement in a declaration and an assignment like that simply because you can put a breakpoint there and inspect the value.

"wow! Python had this feature eight years ago."

Whether or not Phyton has/had these features is not relevant. If you're trying to say that Python is 'better' because it had this feature already, then you're prolly not using either :)

"Good, but someone explain how these 2 features help developers and companies? Also why C# has to be case sensitive?"

These features make the source somewhat more readable. C# has to be case-sensitive because that's what the majority expects. VB.NET is included for people who're having problems with capitalization.

"A" != "a"

Hemadri said...

Yeah, it is really nice feature in C#

Sivaraman said...

very nice feature, it's very use full for us in our code improvement.