How Dynamic Works in C# 4.0?

 Posted by Senthilstayss on 3/14/2013 | Category: C# Interview questions | Views: 5184 | Points: 40

How Dynamic works in C#

DLR (Dynamic language runtime) is set of services which add dynamic programming capability to CLR. DLR makes dynamic languages like LISP, Javascript, PHP,Ruby to run on .NET framework.

To know more about DLR, please visit the following link:-

We are going to see how the Dynamics work in .Net now. For example consider the following statement.

dynamic d=GetSomeData(15); When we execute this statement,Let's see what will happen behind the scenes.
When the above statement executes, the DLR will pass the expression trees to the target object.If the dynamic data type is pointing in memory to a COM object, then the expression tree will be sent to the COM interface named IDispatch. This interface is COM's way of incorporating its own set of dynamic services.
If the dynamic data is not pointing to a COM object, then the expression tree may be passed to the object implementing the IDynamicObject interface.This interface is used to map the expression tree to Ruby specifics in the dynamic languages like Iron Ruby.
If suppose the dynamic data is not pointing to COM object and does not implement IDynamicObject, then the object will be considered as .Net object. In this case the expression tree is sent to the C# run time binder for processing.
After the expression tree has been processed by the given binder, the dynamic data will be resolved to the real in-memory data type after which the correct method is called with any necessary parameters.

To know more about the expression tree basics , please read the following article.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response

More Interview Questions by Senthilstayss