Skip to main content
The dynamic keyword in C# is a static type that instructs the compiler to bypass compile-time type checking. Operations on variables declared as dynamic are deferred until runtime, where the Dynamic Language Runtime (DLR) takes over to resolve types, properties, and method invocations.

Core Mechanics

When the C# compiler encounters the dynamic keyword, it does not emit standard Intermediate Language (IL) instructions for method calls or property access. Instead, it relies on the following mechanisms:
  1. Underlying Type: At the IL level, dynamic is compiled as System.Object. The compiler applies the System.Runtime.CompilerServices.DynamicAttribute to the metadata to signal to the runtime that the member requires dynamic dispatch.
  2. Dynamic Language Runtime (DLR): The compiler emits DLR “call site” payloads. A call site is a caching mechanism that records the runtime type of the object and the operation being performed.
  3. Late Binding: The DLR inspects the object’s actual System.Type at runtime. If the requested member exists and the arguments match, the DLR executes the operation. If the binding fails, it throws a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException.

dynamic vs. object vs. var

Understanding dynamic requires distinguishing it from object and var, as they dictate different phases of type resolution.

Implicit Conversions

The dynamic type supports implicit conversions to and from any other type. The compiler automatically inserts the necessary conversion logic, which is then evaluated by the DLR at runtime.

Technical Limitations

Because dynamic strips away compile-time context, it introduces specific structural limitations:
  • Extension Methods: The DLR cannot resolve extension methods at runtime. Extension methods are a compile-time syntactic sugar that requires the compiler to search static classes in the current using directives.
  • Anonymous Functions: You cannot assign a lambda expression directly to a dynamic variable because lambdas do not have a strict type until they are cast to a specific delegate or expression tree.
  • Local Variable Type Inference: If a method returns dynamic, assigning its result to a variable declared with var results in the local variable also being typed as dynamic.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More