C# Interview Question #137

What is the dynamic keyword in C#?

Reflection, Dynamic & Runtime Senior Advanced

Quick Interview Answer

The dynamic keyword tells the compiler to defer member resolution until runtime.

Detailed Explanation

The dynamic keyword tells the compiler to defer member resolution until runtime.

A dynamic variable can call members without compile-time verification. If the member does not exist or has an incompatible signature, a runtime binding exception occurs.

dynamic can be useful when interacting with genuinely dynamic data or certain interoperability APIs, but it removes compile-time safety and should be used sparingly in normal application code.

Code Example

dynamic value = "Hello";

Console.WriteLine(value.Length);

// This compiles but would fail at runtime:
// value.NonExistingMethod();