New Features - C# 4.0

January 12, 2010 by C#  

The 1st of Jan 2010 I installed Visual C# 2010 Express, quite appropriate date don't you think? (Delayed thanks to my lack of bandwidth among other things, but don't let me get started on that one, sigh)

I know at this point I am a few million years behind writing on this subject, but hey, better late than never.

Nevertheless, I've been having some fun playing around and reading up on all the wonderous new features for a while now - new to C# at least.

The focus of this release (version 4.0), is "Dynamic Programming", which is somewhat code word for "We're compromising to fit/interact with other Tom, Dick & Harry languages". (You guys know who you are)

The new features can be broken up in four groups:

  1. Dynamic lookup
    Allows developers to bypass static type checking (eg dynamic typing), resolution of dynamic types happen during runtime - finally giving C# variables an identity crises ;) :P (Who am I?)

    dynamic a = "Dude"; // I am string
    a = 10; // No, wait I am an int
    a = DateTime.Now; // Oh, now I am a date


    I suspect this is going to become quite the misused feature over the next few years.

    Its great for late-binding and will minimize the amount of code needed in certain cases (eg Type casting) - but potentially quite frivolous.

  2. Named and optional parameters
    Allows developers to specify optional parameters, by giving them default values ( similar to VB, PHP, SQL etc)

    // Notice the optional parameters
    static void test(Int32 i, String s1 = "Test1", String s2 = "Test2")
    {
    	Console.WriteLine(i);
    	Console.WriteLine(s1);
    	Console.WriteLine(s2);
    }
    
    static void Main(string[] args)
    {
    	// Excluding optional parameters
    	test(1);
    
    	// Notice s2, a named parameter
    	test(1, s2: "Test4");
    }


  3. COM specific interop features
    Dynamic types, named and optional parameters will greatly improve interaction with COM, what is more bloated Primary Interop Assemblies (eg Office Integration), wont be needed anymore, instead only needed functionality will be included into the referencing assembly.

    Remember the following? If you've worked with office automation for example, it will look all too familiar...
    object missing = Missing.Value;        
    object newfilename = "a.docx";
    document.SaveAs(ref newfilename, ref missing, ref missing, ref missing,
    	ref missing, ref missing, ref missing, ref missing,
    	ref missing, ref missing, ref missing, ref missing,
    	ref missing, ref missing, ref missing, ref missing);


    If you've ever attempted this in VBA, you'd remember it being a lot cleaner, this is what we see using C# 4.0:
    document.SaveAs("a.docx");


  4. Variance
    C# 4.0 will support safe covariance (allow more derived return type) /contravariance (allow less derived parameter types) via interfaces & delegates.

    Have a look at the following quick example (using delegates), notice the out/in keywords and how they're being used.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    class a { }
    class b : a { }
    
    delegate T covariance<out T>();
    delegate void contravariance<in T>(T value);
    
    class Program
    {
        static void Main(string[] args)
        {
            covariance<b> _b = () => new b();
            covariance<a> _a = _b;
    
            contravariance<a> _a1 = (param) => { Console.WriteLine(param); };
            contravariance<b> _b1 = _a1;
        }
    }


Leave a Comment