Skip navigation

The Big Story in C# 4.0 is Dynamic Programming. Not just Dynamic Typing but also

  • optional and named parameters
  • better COM Support
  • and Co and Contra-variance

The enabling tech for C# 4.0 is the DLR. It provides Dynamic Programming in the .NET environment (CLR). This allows .NET not only to talk to DLR based code, but also JavaScript, COM, Ruby, etc. C# 4.0 and VB.NET 10 will have support for the DLR.

New ‘dynamic’ keyword tells the compiler that an object will be dynamically typed. At compile time the type is dynamic, at runtime the object type is finalized. DLR leverages custom Binders to enable dynamic invocation.

Demonstration of cut and paste from Javascript to C# Changed a few keywords and the Javascript code is now C# code!

How is it done? IDynamicObject, implement an interface and your external application can now be invoked by DLR. For example you can enable a PropertyBag that can be invoked as standard properties. Optional (finally!) and Named Parameters can declare default values for parameters. Enables VASTLY IMPROVED COM Interop! CLR now provides automatic Interop eliminating need for PIA!

Implicit conversion from Dynamic variables to more strongly typed variables.

Demonstration of simplified usage of Office Automation API from C#

.NET arrays are co-variant…but not safely. Generics are not currently co-variant (IEnumerable<String> cannot be used where an IEnumerable<Object> is expected even though String derives from Object). Safe co-variance enables these scenario for output side generics only. Similarly contravariance enables a like structure for input side generics.

Variance supported for interface and delegate types. Supported through statically checked definition-site variance. Not supported in value types. Not supported for out and ref parameters.

Beyond 4.0. Metaprogramming! Using the compiler from the platform. This will enable Metaprogramming REPL, Language Object Model, Embedding DSLs. CSharpEvaluator use the compiler as a service from code. Dynamically compile and run code from code.

Demonstration: declaring a delegate dynamically and invoking it statically.

Demonstration: simple C# console

Demonstration: defining and manipulating a windows form dynamically!

Advertisements

2 Comments

  1. Wow!  Sounds like great features!  I prefer staticly typed languages, so I bet these additions will let me more easliy interop with dynamic languages that are becoming increasingly popular.  I wonder if IE will ever use .NET for JITing Javascript…
     
    A question I have is about "at runtime the object type is finalized".  That sounds like the "var" keyword, but the type is late-bound.  Doesn\’t this still make it statically typed?  Shouldn\’t a dynamic type be able to change to any type at run time?  I\’m sure I\’m missing something.  Where can I find out more! 🙂
     
    Keep us updated,
     
    -Jer

  2. It wasn\’t clear if he meant that it was determined for that specific assignment or if the variable was locked once it\’s assigned.


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: