Thursday, August 11, 2011

Dynamic Dispatch in the .NET 4.0

C# 4.0 supports the dynamic late-binding via the new pseudo-type dynamic keyword. Let's have a quick simple example - dynamic:

Using the dynamic keyword tells the compiler to delay all analysis until run-time thus postponing the resolution and type checking of any member access of such object type.

The pseudo-type dynamic is also treated as System.Object but we do not need to use casting to invoke the methods in the System.Collections.Generic.List in our example. Accessing a member that do not exist in the single table inheritance will of course result to a run-time exception. Say, we try to invoke Length will result to:

As of now (if I'm not mistaken) Java does not support this kind of functionality yet. Personally, I love to see this kind of functionality too in Java =)

Overview of C# 3.0
C# 4.0 New Features in the .NET Framework 4
C# 3.0 Extension Methods and the Java SE 7 Project

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.