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
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.
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!