
The .NET framework 4.0 may be far away now, but Microsoft is working really hard to complete the bits. I wasn’t going to write about this, but after reading the information on the C# future page on MSDN I found it hard not to soak it all in and provide an overview of what is to come.
Dynamic lookup
With the DLR being added to .NET 4.0 Microsoft has the chance to do something that you may think is weird. It is possible to create dynamic objects in C# 4.0. You may already know the var keyword and in C# 4.0 they have added the dynamic keyword.
In essence this keyword means that you create an object and you don’t care about the type during compile time. I mean you REALLY don’t care about the type. The compiler won’t check, so you can break stuff once the application runs.
A good sample of this is the following:
dynamic x = new Duck(); //Compiler will check if duck exists.
x.Quack(); //Does quack exist? Compiler doesn’t check!
The actual lookup is done during runtime using a combination of various lookup methods depending on the type of object that x actually is. So if Duck is a COM object the runtime will use IDispatch from the COM world to resolve the call. If Duck is of type IDynamicObject the runtime will ask the implementation of Duck to resolve the call. (This makes an interesting exercise for developers who love to do dark foodoo in their code!). Last if Duck is just a .NET class the runtime will use the runtime binder (Reflection + something that is not described at this moment) to resolve the call.
Named and optional parameters
Personally I think this feature is the strongest feature added to C# 4.0, Maybe not in terms of runtime possibilities, but I sure think people are going to like this little jewel.
The idea behind optional parameters is pretty simple. Write a method signature like this and you have optional parameters:
Public void Greet(string name = “Bob”) { }
I can now call this method with both Greet(); and Greet(“Joe”); This may look trivial, but I have seen quite a lot of applications that have about 12 overloads for a single method where at least half of them could be removed when parameters are made optional by providing a default value for them.
Together with optional parameters Microsoft has added named parameters. Using named parameters can help make use of methods with optional parameters a whole lot more readable. If you have a method with the signature
Vector3 CreateVector(int x,int y = 0, int z = 0)
You cannot call it like this: CreateVector(3,,20); It’s not readable and the compiler will not support it. Instead you can call the method using the following construct: CreateVector(x:20,z:20) which is a lot more readable.
Where does this lead?
This afternoon I had a discussion with Martijn on how the changes made to C# 4.0 affect developers. We mainly talked about the dynamic features and how dirty this is getting. However I can tell you that it all has a purpose.
One of the great uses of the features described here is the way you can work with COM. Microsoft has essentially copied some features of COM thus improving interop. Also they made working with COM objects easier, since you don’t have to cast the object parameters to their actual type. Declare as dynamic and let the runtime find out what it actually is.
If it quacks like a duck it should be a duck, right?
Conclusion
The next release of .NET will contain a lot of new stuff and also a new CLR (version 4.0). It certainly is interesting to see where it is heading and I can’t wait to get my hands on the bits to try something very daunting *grin*.
Be sure to check out the new features document on the URL posted below as there are a few more bits that are interesting, but too hard to explain here in very little time.
Sources
You can find more information and demos here: http://code.msdn.microsoft.com/csharpfuture