Get Ready For C# 4.0!

Get Ready For C# 4.0!

Rate This
  • Comments 70

Visual Studio 2010 is here! And of course this means that C# 4.0 is also here. Let’s do a quick review of the new language features added in this release.


The dynamic keyword is a key feature of this release. It closes the gap between dynamic and statically-typed languages. Now you can create dynamic objects and let their types be determined at run time. With the addition of the System.Dynamic namespace, you can create expandable objects and advanced class wrappers, and you can provide interoperability between different languages, including dynamic ones. Here is one quick example:

dynamic contact = new ExpandoObject();
contact.Name = "Patrick Hines";
contact.Phone = "206-555-0144";

I have discussed the pros and cons of this feature on this blog. If you want to know more, read here.

Covariance and Contravariance

Variance on generic type parameters in interfaces and delegates is another important feature of this release. It doesn’t add much new functionality, but rather makes things work as you expected them to in the first place. The major advantage is hidden in this simple line, which didn’t compile until C# 4.0:

IEnumerable<Object> objects = new List<String>();

The ability to implicitly convert references for objects instantiated with different type arguments makes it much easier to reuse code. Read the Covariance and Contravariance FAQ to learn more about this feature.

Optional (or Default) Parameters

Looking at the archives of this blog, I see that people have been asking for this feature since C# 1.0. Three versions later, it’s finally here.

Now you can assign a default value to a parameter right within the method declaration. The user of the method can either pass a value or simply skip the argument. In the latter case, the default value is passed to the method.

Method declaration:

public static void SomeMethod(int optional = 0) { }

Method calls:

SomeMethod(); // 0 is used in the method.

Named Arguments

The order of parameters in a method declaration and the order of arguments you pass when calling the method don’t need to match anymore. You can provide arguments in any order you like by specifying parameter names in a method call. This might also improve the readability of your code.

var sample = new List<String>();
sample.InsertRange(collection: new List<String>(), index: 0);
sample.InsertRange(index: 0, collection: new List<String>());

Read more about optional parameters and named arguments on MSDN.

Improved COM Interop

The introduction of the dynamic keyword, optional parameters and named arguments enables improvement of COM interop. So, no more ugly code like this:

var excelApp = new Excel.Application();
// . . .
excelApp.get_Range("A1", "B4").AutoFormat(
    Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing);

You can now simply write the following:

excelApp.Range["A1", "B3"].AutoFormat(

By the way, this code uses one more new feature: indexed properties (take a closer look at those square brackets after Range.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0.

For more information about new COM interop features, once again, refer to MSDN.

What Else?

Of course, C# benefits not only from new language features, but also from improvements to its integrated development environment (IDE) and to the .NET Framework.

Here are some links for further reading:

Happy coding, and thanks to all of you who helped to make this release better by providing feedback!

Leave a Comment
  • Please add 3 and 3 and type the answer here:
  • Post
  • The dynamic keyword also allows for multiple method dispatch or advanced polymorphism and can also be a replacement for Reflection. These concepts are explored in this article <a href="">Type Systems Demystified</a>, which also explains the difference between the dynamic and object type

  • I really like the ability to default parameters:D One of the most missed feature of c++, for me.

  • Optional Parameters feature is cool....

  • dynamic keyword is great in use

  • I see that C# is finally catching up with VB ;-)

  • Please fix a typo

    Covariance and Covariance FAQ  should be Covariance and Contravariance FAQ

  • @partha

    Thanks, fixed.

  • any word on whether datasets support nullable types yet in 4.0?

  • Named Arguments?! unbeliveable!!

  • Optional parameters!! At LAST!!!

    The built in support for memory mapped files (not mentioned above) is also another brilliant addition.

    C# 4.0 is looking truly fantasitc.

  • Can't wait for 4.0.

    optional params at last!

  • I'm not so sure about Named Arguments. Re-arranging defined parameter orders and adding labels to parameter lists doesn't make more readable code for me. In fact I see huge abuses of this functionality that could make it much less readable.

  • C# is supposed to be a standardized language that's not specific to a particular platform (like Windows).  It's clear that this dynamic keyword was added /mostly/ for COM interop... A Windows Only feature.  COM will go away one day.  COM doesn't exist on any other platforms that DO support C#.  You don't modify a language because of ONE old and fading feature of ONE O/S.

    This feature seriously degrades the language and it absolutely, positively, WILL, beyond the shadow of ANY doubt, be abused rampantly, not just by bad coders, but by NEW coders as well as cross-coders (coders who's expertise is in other, dynamic languages).

    Our public resources of C# code will now start being filled with poorly written code that abuses this feature.  This makes it ALL of our problem.

    It's not a matter of whether WE choose to use the dynamic feature or not, it's a matter of everyone existing in the same "ecosystem" where we're going to HAVE to deal with maintaining poorly written code that abuses this feature.

    I rejoiced when I no longer had to deal with "variant".  This was a HORRIBLE decision and a poorly made one at that.


    Aside from that (and that's a HUGE "aside"), the other features I've seen are awesome and I'm already using some of them, but I do regret the decision to include "dynamic" because I -=>KNOW<=- how badly it's going to be abused.

    Please people, AVOID using dynamic types UNLESS you're using COM interop.  Actually, I'd prefer that NOBODY used it for ANY reason to improve the chances of them ripping this monster out of a later language update.  I can't believe that ECMA would have accepted this feature into the language!


  • Covariance and Contravariance - Eureka!!!

  • Mr C Sharpner is making some rather ignorant statements. For one, the "dynamic" functionality has existed in VB.Net since V1.0. Although VB.Net uses the idea of late binding using "option explicit off", it is the same thing. Therefore, doing COM interop was much easier in VB.Net and it did not in itself cause major problems. I personally worked on VB.Net projects and we always used option explicit except where COM interop was needed, where we would turn it off. Until C# added the "var" type, it was extremely difficult to do work with certain COM types that did not export thier types correctly.

    Secondly, the complaint that it somehow pollutes the C# language for other platforms shows an ignorance of the real world of software. Microsoft makes C# for Windows and that's where it is going to provide support and features. There is a need for COM interop, and for WinAPI (Pinvoke) support. Nearly all C# code is written to run on Windows, except for the half a dozen or so programmers who are using Mono.

Page 1 of 5 (70 items) 12345