C# dynamic KeywordUse the dynamic keyword to specify a type is not known at compile-time. Inspect C# code and IL.
Dynamic. The dynamic keyword influences compilation. A dynamic variable can have any type. Its type can change during runtime. The downside is that performance suffers and you lose compile-time checking.
Info Dynamic is advanced functionality. It can be useful. But usually it should be avoided.
Example. The dynamic keyword is used to replace other types such as int, bool or string. In this example, we assign the value 1 to the dynamic variable a. This means "a" is of type System.Int32 at that point.
Nonexistent members. The compiler allows you to call nonexistent properties, fields and methods on a variable of type dynamic. You can see that the field _y is clearly a string, but there is no Error property on String.
And With dynamic, this results in a run-time error. Without dynamic, we would get a compile-time error.
C# program that uses dynamic type
static dynamic _y;
static void Main()
// Use dynamic local.dynamic a = 1;
// Dynamic now has a different type.
a = new string;
// Assign to dynamic method result.
a = Test();
// Use dynamic field.
_y = "carrot";
// We can call anything on a dynamic variable.// ... It may result in a runtime error.
static dynamic Test()
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
'string' does not contain a definition for 'Error'...
In Visual Studio 2010, try hovering over the "WriteLine" method call in the program. You will see it displays "(dynamic expression) This operation will be resolved at runtime." This means you are not actually calling WriteLine directly.
Instead You are calling something named WriteLine at runtime and passing it one argument.
Code generated for above example, C#:
private static class <Main>o__SiteContainer0
public static CallSite<Action<CallSite, Type, object>> <>p__Site1;
public static CallSite<Action<CallSite, Type, object>> <>p__Site2;
public static CallSite<Action<CallSite, Type, object>> <>p__Site3;
Internals. Dynamic is implemented with compile-time transformations by the C# compiler. You won't find any "dynamic" types in the virtual execution engine. Instead, the C# compile builds up an alternate representation of your dynamic code.
Then, it outputs that to the metadata. Looking at the IL, we see that the dynamic keyword results in a custom class being generated by the compiler. This class is contains fields for each of the "sites" where dynamic is used.
Next, we can look at the call sites themselves. At each call site, the site field from the above static class is instantiated. Then, the Target method on the site field is called. The method "WriteLine" is specified as a string.
Info The class we are calling (Console) is specified as a Type pointer with typeof.
Summary. The dynamic keyword provides access to the Dynamic Language Runtime, a layer of functionality on top of the Common Language Runtime. This basically means dynamic is implemented with a lot of inserted code that simply uses the CLR.