TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Dynamic Keyword

This C# article reveals how to use the dynamic keyword. Example C# code and IL is provided.

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. It erases many benefits of the C# language.

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.

Int

Next, we assign it to a string array. The type again changes. You can also use dynamic as a return type (or formal parameter type). Finally, we use a dynamic static field, and call a nonexistent property on it.

String Array

Based on:

.NET 4

C# program that uses dynamic type

using System;

class Program
{
    static dynamic _y;

    static void Main()
    {
	// Use dynamic local.
	dynamic a = 1;
	Console.WriteLine(a);

	// Dynamic now has a different type.
	a = new string[0];
	Console.WriteLine(a);

	// Assign to dynamic method result.
	a = Test();
	Console.WriteLine(a);

	// Use dynamic field.
	_y = "carrot";

	// You can call anything on a dynamic variable,
	// ... but it may result in a runtime error.
	Console.WriteLine(_y.Error);
    }

    static dynamic Test()
    {
	return 1;
    }
}

Output

1
System.String[]
1

Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
    'string' does not contain a definition for 'Error'...

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.

Compile-Time 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.

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.

Code generated for above example: C#

[CompilerGenerated]
private static class <Main>o__SiteContainer0
{
    // Fields
    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;
}

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.

Typeof

Code generated at call sites: C#

if (<Main>o__SiteContainer0.<>p__Site1 == null)
{
    <Main>o__SiteContainer0.<>p__Site1 = CallSite<Action<CallSite, Type, object>>.Create(
	Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "WriteLine", null,
	typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(
	CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType,
	null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
}
<Main>o__SiteContainer0.<>p__Site1.Target(<Main>o__SiteContainer0.<>p__Site1,
    typeof(Console), a);

Type. The dynamic keyword is used as a type in the C# language, but it is not a type in the underlying intermediate language. Thus, it can be considered a high-level type but not a low-level type.

Note: Most constructs in the C# language correspond fairly closely to their underlying representation.

Tip: Other exceptions to this rule include the yield return construct and the params keyword.

YieldParams

Uses. It is definitely a bad idea to use dynamic in all cases where it can be used. This is because your programs will lose the benefits of compile-time checking and they will also be much slower.

Note: All the code generated by the C# compiler would yield a program that is many times slower than a regular C# program.

Compiler

There are some current uses of dynamic. It can make poorly-designed external libraries easier to use: Microsoft provides the example of the Microsoft.Office.Interop.Excel assembly.

And: With dynamic, you can avoid a lot of annoying, explicit casting when using this assembly.

Excel

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf