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.
Tweet
22 comments:
wow! Python had this feature eight years ago.
@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?
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
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?
Are there any other feature additions made in 4.0?
@Bitme
Yes there are. I will be posting short articles here about some of the other new features here. Stay tuned.
Just wondering ... why do you use the explicit "string name = null" instead of "string name = SomeMethod(...)"? It doesn't really add any security ...
I just coded it that way for the example. Both ways will work fine.
Did you really mean to say that your age is just 20..?
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.
Yes this feature has been in VB for a while now. It's nice to see C# catching up!
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)
Finally VB'ers get their day in court, LOL
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.
This is a wonderful information about compiler filling in the optional parameter.
Thanks
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.
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.
Very interesting and useful
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.
"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"
Yeah, it is really nice feature in C#
very nice feature, it's very use full for us in our code improvement.
Post a Comment