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.



Will you give this article a +1 ? Thanks in advance




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

comments

23 Responses to "Two New Features of C# 4.0"
  1. Anonymous said...
    August 6, 2009 at 9:19 AM

    wow! Python had this feature eight years ago.

  2. Anonymous said...
    August 6, 2009 at 11:32 AM

    @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?

  3. Anonymous said...
    August 6, 2009 at 12:03 PM

    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

  4. Anonymous said...
    August 6, 2009 at 11:52 PM

    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?

  5. Bitme said...
    August 7, 2009 at 1:36 AM

    Are there any other feature additions made in 4.0?

  6. Malcolm Sheridan said...
    August 7, 2009 at 2:26 AM

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

  7. Anonymous said...
    August 7, 2009 at 3:04 AM

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

  8. Malcolm Sheridan said...
    August 7, 2009 at 5:06 AM

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

  9. dncdude said...
    August 8, 2009 at 3:54 PM

    Nice article. I wrote about Optional Params here http://www.dotnetcube.com/post/Overview-of-NET-40-features-e28093-C-40-Named-and-Optional-Parameters.aspx

  10. Sheron said...
    August 9, 2009 at 10:12 AM

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

  11. Anonymous said...
    August 9, 2009 at 3:27 PM

    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.

  12. Malcolm Sheridan said...
    August 9, 2009 at 7:43 PM

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

  13. Anonymous said...
    August 10, 2009 at 4:07 AM

    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)

  14. Anonymous said...
    August 10, 2009 at 8:43 AM

    Finally VB'ers get their day in court, LOL

  15. Anonymous said...
    August 17, 2009 at 3:09 AM

    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.

  16. Anonymous said...
    August 18, 2009 at 11:46 AM

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

    Thanks

  17. Anonymous said...
    August 21, 2009 at 7:00 AM

    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.

  18. Anonymous said...
    August 24, 2009 at 4:08 PM

    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.

  19. Anonymous said...
    August 31, 2009 at 12:06 AM

    Very interesting and useful

  20. Matt said...
    September 1, 2009 at 11:13 AM

    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.

  21. Anonymous said...
    September 11, 2009 at 6:59 AM

    "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"

  22. Hemadri said...
    October 26, 2009 at 9:12 PM

    Yeah, it is really nice feature in C#

  23. Bharath said...
    September 7, 2010 at 10:44 PM

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

 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions