Discusses the new features of C# 4.0 including dynamic lookup, covariance/contravariance generics, named and optional paramters
The .NET framework 4.0 CTP has just been released, and I think it’s a good time to explore the new features of C# 4.0. In this post, I will introduce about the following features: dynamic lookup, generics covariance and contravariance support, optional and named parameters.
If var in C# 3.0 brings about type inference for local variables with the purpose of saving a couple of key strokes, dynamic lookup adds much more to the dynamicity of the C# language. When you declare a variable with dynamic , all of its method invocations or member accesses will be resolved at runtime. For example, let’s look at the following code:
In the code, we create a string object, and instead of
assigning it to a variable of type
, we assign it to a
variable declared as
. This basically
instructs the compiler to not attempt to resolve any method call or member
access of the declared variable. Instead, all these resolutions will happen at
runtime. That being done, the next line in which we invoke a non-existing
method with an arbitrary parameter will compile just fine into CIL. At runtime,
when the method call is resolved and the runtime finds that there’s no such
method in the runtime type (which is a
), an exception is thrown. That being said, if we use a
valid method, then there would be no exception and the code will run fine till
completion. For example:
We basically create two anonymous types, for the sake of convenience, both having a property called ID, and then instantiate objects out of these types. We then pass each of these two objects to the method PrintID, which accepts a dynamic object and prints out the ID property. The code will print out “111” and “101”, respectively. And yes, we’ve just seen duck typing in action. In C#.
That example also implies an interesting usage of dynamic with regard to anonymous types. Now, we can pass objects of anonymous types out of their declaration scope (e.g., the method) and still be able to invoke their methods or access their members without resorting to verbose Reflection code.
We are not just limited to making statically typed .NET
objects become dynamic, we can use this dynamic lookup feature to conveniently
interact with “actual” dynamic objects available through the Dynamic Language
Runtime (DLR) included in .NET framework 4.0. In fact, we can implement such
dynamic objects in C# by implementing the
interface, which is also part of the DLR. Regardless of the actual
receiver of dynamic dispatch, the usage of dynamic lookup is exactly the same
from the caller’s perspective.
Some of you might wonder what code the compiler generates and whether there is any change to the CLR to support dynamic lookup or not. To answer that question, let’s look at the CIL generated from the very first code fragment of this post. (Click on the image to see the full size.)
The generated CIL is pretty verbose, but a skim through it
reveals two important things. Firstly, our “dynamic variable” turns out to be a
plain old CLR
instance. And secondly, there’s no new CIL directive or opcode to
support dynamic lookup. Instead, dynamic resolution and invocation is
completely handled by framework code. In fact, the above CIL is equivalent to
the following C# code, but now, without the
(In fact, I simplified the code a bit so that it can fit in
one method. The actual CIL generated does have a check at
, which basically looks at the
static field of a
nested class, which is generated automatically by the compiler to see if it’s
or not before going ahead and initializing it. In order words, the
is cached for
subsequent invocations of the same method.)
So, there’s really no magic behind the
keyword. What the compiler basically does is generate some payload
containing the information about the invocation so that it can be made at
runtime. If there’s any magic at all, it would be in the static method
which uses Reflection to invoke the
on the given object if it’s not an instance of
Now, we know that a “dynamic object” is actually a plain
old object; it should explain why a method can accept dynamic parameters. It
should also be of no surprise to know that dynamic lookup can also be applied
to the return value of an instance/static method or an instance/static field.
After all, unlike the
keyword which requires the compiler to infer the exact type at
compile time, the compiler in dynamic lookup scenarios can simply pick
as the type.
In previous versions of C#, generic types are invariant.
That is, for any two types
, in which
is a subclass of
is a subclass of
have no inheritance relationship whatsoever to each other. On the
other hand, if
is a subclass of
and if C# supports
is also a subclass of
, or if C# supports
is a superclass of
To understand why pre-4.0 C# disallows covariance and contravariance, let’s look at some code:
The code in line 2 can be error-prone. Consider what we can write after line 2, assuming it is allowed:
On the other hand, if C# supported contravariance, we could have written the following problematic code:
Due to this invariant restriction, although for the good
purpose, we can’t easily reuse variables and methods to respectively get
assigned to or accept various generic types. It’s somewhat unfortunate because
the key thing to realize is that covariance is fine as long as
does not have any
method or member accepting arguments of type
(e.g., if we can’t add
a bunch of objects into the
, which is indeed an instance of
, we’ll be safe).
Besides, contravariance is just as safe if
does not appear in any
return value from a member or method (e.g., if we can’t get any string out from
, which is indeed an instance of
, we’ll be safe).
Fortunately, C# 4.0 provides us an option: if a
as its type parameter
and does not have any method or member that takes in a parameter of type
, we can declare it to be covariant on
. On the other hand, if that interface or delegate does not have
any method or member that returns
, we can declare it to be contravariant on
. (Notice the emphases, only interfaces and delegates have
covariance and contravariance support, and the type parameter must be a
reference type. On the other hand, C# arrays have been supporting covariance
from the very beginning.)
Let’s look at an example. We have a
basically returns random instances of objects of a certain type (e.g.,
). Its declaration is as follows:
Because this delegate does not take in any
as its argument, we
can safely make it covariant on
. And indeed, the compiler will allow us to do so by adding
before the type parameter. However, if this delegate is declared
T Generator<out T>(T
, then the compiler will complain since it’s no longer safe for
covariance. Now, let’s look at the usage:
With contravariance, you need to use the in keyword. Let’s look at an example that makes use of both contravariance and covariance:
This converter takes an object of type
and converts it to an
object of type
). Since it does not take in any
, it can safely be declared to be covariant on
. Similarly, since it exposes no
, it can safely be
declared to be contravariant on
. Its usage is as follows:
Note that although the above examples show how to declare covariance and contravariance for delegate types, it’s not different to do so with interfaces.
Before we finish with covariance and contravariance, let’s
look at what code the compiler generates. After all, we know that the compiler
must encode something to the CIL to denote covariant and/or contravariant
generic types so that they can be consumed properly by client code. And, this
is what the definitions of our
delegates are when viewed from ILDASM.
A final thought about this feature. While this is a good enhancement to the language, I like the generics covariance/contravariance implementation, enabled via wildcards, in Java, better for its flexibility. Anyway, let’s just be happy with it for now, we can’t have everything.
The last two features of C# 4.0 that we’ll discuss about are optional parameters and named parameters. These features have been with VB.NET since forever, and I’m glad they are finally implemented in C#.
With optional parameters, we can provide default values for methods’ and constructors’ parameters. That way, we don’t have to write a bunch of overloaded methods and constructors. For example, we can define a constructor like this:
Now, you can invoke this constructor with any of these calls:
How exactly does C# implement this feature? If we look at the CIL generated for this code, we’ll see there’s no magic at all. Basically, the default values will be injected to the call site so that the method invocation happens normally. Here’s the CIL generated by the compiler:
.paramdirective which basically tells the compiler about the default values for the optional parameters. These parameters are also annotated by the
While being a great feature, there’s a caveat when using optional parameters: since the compiler inlines the default values at the caller site, changes to default values at the library site won’t be reflected unless the caller is recompiled. In order words, you should consider default values of parameters as part of the published API of a method, and you’d better make them right the first time.
Now, let’s say we want to call the
’s constructor with the
specified but not the
, how would we do that?
To avoid ambiguity (for example, when two optional parameters are both strings), C# won’t allow us to simply skip a parameter like this:
An approach C# could take is to allow this syntax:
However, this looks terrible enough with just one missing
parameter, much less than with more of them. (How do you like to read this
For this particular situation, named parameters is an excellent solution:
This is not the only use of named parameters though. A very important purpose of named parameters is to enhance the readability of code. Let’s say you have a class having a bunch of fields that are to be initialized in a constructor. Without named parameters, a constructor of such a class will look very ugly, and will be hard to understand without resorting to the documentation, source code, or Intellisense.
One approach is to create a builder to create an instance of such a class. For example:
This will make the code more explicit about what values are to be assigned to what fields. However, the drawback is that we need to implement a builder class, which is quite a tedious task if we have to repeat that for many classes in our application.
With C# 3.0, it’s not that bad because we can use an object initializer to do something like this:
This looks good, but we have to define all the necessary properties for it to work, which is also a tedious thing to do if we don’t really need properties for the class, or if the class itself is immutable.
With named parameters, we have a very nice solution without having to write any builder or property if we don’t want to. (Note the semicolon after the field name.)
Regarding the implementation of this at the CIL level, the compiler is smart enough to infer the correct argument order and simply perform a plain old method invocation.
That’s it. The key features of C# 4.0. I am personally glad that C# has come to support these. Some people say that C# has become so complex and has started losing its original beauty. While I can understand that view, I think the situation is not that bad. While it’s obvious that C# has more and more constructs to support functional and dynamic programming, the statically typed nature and the old constructs of the language are still there, and no developer is forced to use the new features if they don’t need to. On the other hand, these features bring more options for those who need them, and I’d rather have more options than to be handcuffed.