New Features in C# Part III (C# 4.0)

[Related Posts: Part I (C# 2.0) and Part II (C# 3.0)]

(Based on the draft version of the C# 4.0 specification which are subject to change.)

Just like previous releases of C# had a major theme (think generics for C# 2.0 and LINQ for C# 3.0), the new major theme for this release is to add dynamic programming features to the C# language.

1. Dynamic lookup: A new type has been introduced called dynamic. The idea is that you can call any operation on the dynamic type you want, the compiler won’t check it at compile-time, but figure it out at runtime. A dynamic object is assumed to support any operation at compile-time, only at runtime will you get an error. The result of any dynamic operation is of type dynamic.

        // Instantiation
        dynamic d = 7; // implicit conversion
        dynamic d = GetObject();
        // Operations on dynamic types
        d.f = d.P;           // getting and settings fields and properties
        d[“one”] = d[“two”]; // getting and setting thorugh indexers
        int i = d + 3;       // calling operators

2. Named and optional parameters: Now declare optional parameters simply by providing a default value for it. And call params by name: like so,

        // Definition of the method
        public void Method(int x, int y = 1, int z = 2){}

        // Using the optional params
        Method(1);          // same as Method(1, 1, 2);
        Method(1, 2);       // same as Method(1, 2, 2);
        Method(1, 2, 3);

        // Named params called, note arbitrary order and missing y
        Method(z: 1, x: 2);

3. Improvements to COM interoperability: A bunch of changes to facilitate COM interop has been introduced, like passing values to reference params. Unlike in C#, COM APIs use ref params simply as a way to pass value type and make no assumptions  about mutability. Thus, specifically for COM methods we are allowed to do this,

        // For this COM method signature,
        void Method(ref int i, ref int j) { }

        // Previously we would have to do this,
        int x = 0;
        int y = 0;
        Method(x, ref y);

        // Now we can pass in y
        Method(x, y);  // But note that changes to x inside the method will be discarded.
                       // Changes to y will be retained.

4. Covariance and contravariance of generics: Covariance allows casting of generic types to the base types, for example, IEnumerable<A> will be implicitly convertible an IEnumerable<B> if A can implicitly be converted to B.

        // Say we have a List of strings
        IList<string> strings = new List<string>();

        // We can now convert it into an Enumerable collection
        IEnumerable<object> objects = strings;

For this IEnumerable will be marked with the out modifier in C# 4.0,

        public interface IEnumerable<out T> : IEnumerable {
            IEnumerator<T> GetEnumerator();

Contravariance allows for example, IComparer<A> to be cast to IComparer<B> even if B is a derived type of A. For this IComparer will be marked with the in modifier.

        public interface IComparer<in T> {
            public int Compare(T left, T right);

Digg This

About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

5 Responses to New Features in C# Part III (C# 4.0)

  1. Pingback: New Features in C# Part I (C# 2.0) « I.Net

  2. Pingback: New Features in C# Part II (C# 3.0) « I.Net

  3. Jeff says:

    #3 is a bit confusing. Is it possible you’ve made a typo?

    The signature
    “void Method(ref int x, ref int y) {}”
    contains no difference between the parameters x and y. Why would x be treated differently from y when the call
    “Method(x, y)”
    is made? Why would it retain the changes to y and discard the changes to x?
    Should the method signature be
    “void Method(int x, ref int y)”
    for your example?

  4. saurabh says:


    What i liked about the article it is short, informative and concrete.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: