53

I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject and ExpandoObject types. It seems like DynamicObject is used e.g. when you want to access variables from Python scripts and ExpandoObject when talking with COM/Office objects. Am I right? What is the difference in their use?

Palec
  • 12,743
  • 8
  • 69
  • 138
Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
  • 2
    Better answers here: http://stackoverflow.com/questions/3565481/differences-between-expandoobject-dynamicobject-and-dynamic – Brian Low Mar 01 '14 at 22:56

1 Answers1

97

Expando is a dynamic type to which members can be added (or removed) at runtime. dynamic is designed to allow .NET to interoperate with types when interfacing with dynamic typing languages such as Python and JavaScript.

So, if you need to handle a dynamic type: use dynamic and if you need to handle dynamic data such as XML or JSON: use ExpandoObject

The declaration of an expando shows the relationship between dynamic and the expando:

dynamic expando = new ExpandoObject();

And the ability to add a new property:

expando.SomeNewStringVal = "Hello World!";

That last line of code creates a brand new string property in the expando object called SomeNewStringVal. The string type is inferred from the assignment.

So an expando is a dynamic data type that can represent dynamically changing data. That's it in a nutshell. Here's a deeper look at dynamic and expando.

Complete example:

using System;
using System.Dynamic;

class Program
{
    static void Main(string[] args)
    {
        dynamic expando = new ExpandoObject();
        expando.SomeNewStringVal = "Hello Brave New Whirrled!";
        Console.WriteLine(expando.SomeNewStringVal);

        // more expando coolness/weirdness:
        var p = expando as IDictionary<String, object>;
        p["A"] = "New val 1";
        p["B"] = "New val 2";

        Console.WriteLine(expando.A);
        Console.WriteLine(expando.B);
    }
}
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • 7
    Though he has accepted it, I think the OP meant difference between DynamicObject and ExpandoObject. – nawfal Jul 30 '15 at 06:46
  • 1
    Just to note this does come in handy at time in the real world. I just used it out API to create dyanmic response objects before serializing to xml or json. This way I can recursively add sub classes with distinct property names without having to define a ton of explicit properties. "GroupsL1": [{ "GroupingL2:: [{... – mBrice1024 Jul 20 '16 at 16:09
  • 2
    [`DynamicObject`](https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject?view=netframework-4.7) was left unaddressed. MSDN: *Provides a base class for specifying dynamic behavior at run time. This class must be inherited from; you cannot instantiate it directly.* The MSDN page contains a simplified `ExpandoObject` implementation based on `DynamicObject`. So, both the types are used with late binding, but `ExpandoObject` is concrete (and sealed), while `DynamicObject` is just a base class for custom dynamic objects. – Palec Aug 26 '17 at 19:26