C# 4 and the dynamic keyword - Whirlwind Tour around .NET 4 (and Visual Studio 2010) Beta 1 May 20, '09 Comments [15] Posted in DLR | Learning .NET | TechEd Sponsored By I've posted twice so far on .NET 4, first on ASP.NET 4, then on improvements in C# around dynamism and PIAs as well as the COM Binder. Now "dynamic." So I asked this guy, what's up with the dynamic keyword, and what type was it exactly? I mean, C# isn't dynamic, right? He says: "Oh, well it's statically-typed as a dynamic type." Then my brain exploded and began to leak out my ears. Honestly, though, it took a second. Here's a good example from some of Ander's slides: Calculator calc = GetCalculator();int sum = calc.Add(10, 20); That's the creation of an object, invokation of a method, and the collection of a return value. This is the exact same code, as the "var" type is figured out at compile time. var calc = GetCalculator();int sum = calc.Add(10, 20); If you wanted to do the exact same thing, except with Reflection (like if it were some other class, maybe old-COM interop, or something where the compiler didn't know a priori that Add() was available, etc) you'd do this: object calc = GetCalculator();Type calcType = calc.GetType();object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });int sum = Convert.ToInt32(res); It's pretty horrible to look at, of course. If the object is some dynamic thing (from any number of sources), we can do this: dynamic calc = GetCalculator();int sum = calc.Add(10, 20); And get the dynamic method invocation and conversion of the return type. Basically it looks just like we're calling any other object. Dynamism? Here's the differences you see while coding. Hovering over the keyword gives me this nice tooltip. When I hit the "." expecting intellisense to save me from my ignorance: I'm told this is a dynamic expression that will be resolved at runtime. Here's a C# program calling a method in a python (.py) file: ScriptRuntime py = Python.CreateRuntime();dynamic random = py.UseFile("random.py");//Make an array of numbersvar items = Enumerable.Range(1, 7).ToArray();random.shuffle(items); Here we're passing in an array if ints (System.Int32[]) into the Python 'shuffle' method and it works just fine. The DLR basically enables everyone to talk to everyone. That includes not just Python and Ruby, but Silverlight, Office/COM, and others. What price REPL? John Lam has a great post about his TechEd talk where he took a spin on a traditional REPL (READ-EVAL-PRINT-LOOP) using the DLR. He even allows switching back and forth between languages, which is odd/interesting. John's even put the code for his REPL up on GitHub. Why is this interesting? Well... He took his REPL and embedded it into an example Open Source app, specifically Witty, a WPF Twitter Client. Why he didn't use BabySmash is beyond me. ;) Check it out, as well as the source code diff for Witty on John's blog. It'll be nice to have this kind of dynamic stuff just baked in and waiting for me to use it. « The Weekly Source Code 42 - Tree Trim, P... | Blog Home | Back to Basics: Using Fusion Log Viewer ... » About Scott Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author. About Newsletter Sponsored By Hosting By