Take the 2-minute tour ×

What are the differences between System.Dynamic.ExpandoObject, System.Dynamic.DynamicObject and dynamic?

In which situations do you use these types?

share|improve this question

2 Answers 2

up vote 78 down vote accepted

The dynamic keyword is used to declare variables that should be late-bound.
If you want to use late binding, for any real or imagined type, you use the dynamic keyword and the compiler does the rest.

When you use the dynamic keyword to interact with a normal instance, the DLR performs late-bound calls to the instance's normal methods.

The IDynamicMetaObjectProvider interface allows a class to take control of its late-bound behavior.
When you use the dynamic keyword to interact with an IDynamicMetaObjectProvider implementation, the DLR calls the IDynamicMetaObjectProvider methods and the object itself decides what to do.

The ExpandoObject and DynamicObject classes are implementations of IDynamicMetaObjectProvider.

ExpandoObject is a simple class which allows you to add members to an instance and use them dynamically.
DynamicObject is a more advanced implementation which can be inherited to easily provide customized behavior.

share|improve this answer
What would be a good place to learn more about this ? Not the API but the why behind the API ? e.g. Why doesn't ExpandoObject derive from DynamicObject, which looks the defacto base type for ruby's 'method_missing' based programming. –  Gishu Jun 14 '12 at 6:26
Could you add some usage examples where possible? For instance, how would i use a DynamicObject, and what are the benefits? –  oɔɯǝɹ Dec 30 '14 at 10:03
Great answers without examples like this are like cake without cream on top. –  Teoman shipahi Apr 28 at 16:24

According to the C# language specification dynamic is a type declaration. I.e. dynamic x means the variable x has the type dynamic.

DynamicObject is a type that makes it easy to implement IDynamicMetaObjectProvider and thus override specific binding behavior for the type.

ExpandoObject is a type that acts like a property bag. I.e. you can add properties, methods and so forth to dynamic instances of this type at runtime.

share|improve this answer
dynamic is not an actual type... it's just a hint to tell the compiler to use late binding for this variable. dynamic variables are actually declared as object in MSIL –  Thomas Levesque Aug 25 '10 at 12:57
@Thomas: from the compiler's point of view it is a type, but you're right that the runtime representation is that of Object. You'll find the statement "statically typed to be dynamic" in several MS presentations. –  Brian Rasmussen Aug 25 '10 at 13:33
@Thomas: and the language spec says "C# 4.0 introduces a new static type called dynamic". –  Brian Rasmussen Aug 25 '10 at 13:35
indeed... But I think it's confusing to consider it as a type, since there is no inheritance relation with types like DynamicObject or ExpandoObject –  Thomas Levesque Aug 25 '10 at 14:32
@NathanA I'm with you here. However, the language specification calls it a type, so that's what I'm going with. –  Brian Rasmussen Apr 10 '14 at 16:04

Your Answer


