Skip to content

dynamic Type

Question

Describe the dynamic type. How does it differ from an anonymous type, and can one dynamic variable hold different data types?

Short interview answer

dynamic defers member binding and type checks to runtime. An anonymous type is compiler-generated, strongly typed, and has a fixed shape. A dynamic variable can be assigned values of different runtime types, but invalid operations fail at runtime.

Detailed answer

dynamic is useful mainly for COM interop, dynamic-language integration, or genuinely dynamic object models. The compiler does not validate the member call, so a missing member or wrong arguments produce a runtime binder failure. An anonymous type is not dynamic: its members are inferred at compile time and cannot be changed after creation.

Sources