Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages and Disadvantages of C# 4.0 'dynamic' keyword? [closed]

I have learned and checked the advantages of dynamic keyword in C# 4.

Can any body tell me the disadvantages of this. Means dynamic vs Var/Object/reflection ???

Which thing is batter more. Is dynamic more powerful at run time??

like image 375
Waheed Avatar asked Nov 20 '11 17:11

Waheed


People also ask

What is the disadvantage of C?

Drawbacks & Disadvantages of C Language Therefore, it does not support the OOPs concept, i.e., object-oriented programming concept. Exception handling is missing in the C programming language. It is an essential tool for software developers to track exact errors in the code, i.e., syntax or logical.

What are the advantages of C and C++?

Benefits of C++C++ has a rich function library. C++ allows exception handling, and function overloading which are not possible in C. C++ is a powerful, efficient and fast language. It finds a wide range of applications – from GUI applications to 3D graphics for games to real-time mathematical simulations.

What are major disadvantages of machine code in C?

It is machine dependent i.e. it differs from computer to computer. It is difficult to program and write. It is prone to errors • It is difficult to modify. It is a low level programming language that allows a user to write a program using alphanumeric mnemonic of instructions.


1 Answers

One of the most interesting thing with dynamic keyword which you can do is : implementation of multiple-dispatch.

Here is an example of double-dispatch (a specific case of multiple-dispatch). In OOP, at runtime a particular implementation of virtual (or abstract) method is called, based on the runtime type of one object which is passed as this to the member function. That is called single-dispatch, as the dispatch depends on the runtime type of single object. In double-dispatch, 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 one object.

like image 140
Nawaz Avatar answered Sep 20 '22 21:09

Nawaz