Advantages and disadvantages of the C # 4.0 'dynamic' keyword?

I have studied and tested the benefits of the dynamic keyword in C # 4.

Can any body tell me about these shortcomings. dynamic vs Var / Object / reflection values

Which thing is more. Is dynamic more powerful at runtime?

+5
c # dynamic
Nov 20 '11 at 17:37
source share
2 answers

Not exactly var vs dynamic, but the following SO link discusses reflection and dynamics. Departure: dynamic vs Var / Object / reflection

+2
Nov 20 '11 at 17:45
source share

One of the most interesting things with the dynamic keyword that you can do is implement a dual send. In OOP, a specific implementation of a virtual (or abstract) method is called at run time based on the runtime type of a single object, which is passed as this to a member function. This is called a single dispatch, since dispatch depends on the type of runtime of a single object. In double submission, it depends on the type of two objects.

Here is one example:

 public abstract class Base {} public class D1 : Base {} public class D2 : Base {} public class D3 : Base {} public class Test { public static void Main(string[] args) { Base b1 = new D1(); Base b2 = new D2(); Method(b1,b2); //calls 1 Method(b2,b1); //calls 1: arguments swapped! } public static void Method(Base b1, Base b2) // #1 { dynamic x = b1; dynamic y = b2; Method(x,y); // calls 2 or 3: double-dispatch - the magic happens here! } public static void Method(D1 d1, D2 d2) // #2 { Console.WriteLine("(D1,D2)"); } public static void Method(D2 d2, D1 d1) // #3: parameters swapped { Console.WriteLine("(D2,D1)"); } } 

Output:

 (D1,D2) (D2,D1) 

That is, the actual method is selected at runtime based on the runtime type of two objects, as opposed to a single object.

+13
Nov 20 '11 at 17:45
source share



All Articles