Skip to content

Values Held by dynamic

Question

Can a single variable of a dynamic type store different types of data?

Short interview answer

Yes. A dynamic variable can be assigned values with different runtime types. The member operations performed on it are resolved at runtime.

Detailed answer

For example, a variable may first hold a string and later an object returned by a COM API. The compiler permits calls without validating the target's members; invalid calls fail at runtime. That flexibility is why dynamic should be constrained to boundaries where the shape cannot be represented statically.

Sources