Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A

.NET interview questions: - What is Dynamic keyword and mention its practical uses?

Following is the .NET interview questions on dynamic keyword in .NET.

Programming languages can be divided in to two categories strongly typed and dynamically typed.  Strongly typed languages are those where the checks happen during compile time while dynamic languages are those where type checks are bypassed during compile time. In dynamic language object types are known only during runtime and type checks are activated only at run time.

                                                dynamic 1

So we would like to take advantage of both the world. Because many times we do not know object type until the code is executed. In other words we are looking at something like dynamically statically typed kind of environment. That’s what dynamic keyword helps us with.
If you create a variable using the “Dynamic” keyword and if you try to see members of that object   you will get a message as shown below “will be resolved at runtime”.
dynamic 2

Now try the below code out. In the below code I have created a dynamic variable which is initialized with a string data.  And in the second line I am trying to have fun by trying to execute a numeric incremental operation.  So what will happen now ?.... think.

dynamic x = "c#";

Now this code will compile fine without any complains. But during runtime it will throw an exception complaining that the mathematical operations cannot be executed on the variable as it’s a string type. In other words during runtime the dynamic object gets transformed from general data type to specific data type (ex: - string for the below code).

dynamic 3

Practical uses of Dynamic keyword: -

One of the biggest practical uses of dynamic keyword is when we operate on MS office components via interop.

So for example if we are accessing Microsoft excel components without dynamic keyword, you can see how complicated the below code is. Lots of casting happens in the below code, right.

// Before the introduction of dynamic.
Application excelApplication = new Application();
((Excel.Range)excelApp.Cells[1, 1]).Value2 = "Name";
Excel.Range range2008 = (Excel.Range)excelApp.Cells[1, 1];

Now look at how simple the code becomes by using the dynamic keyword. No casting needed and during runtime type checking also happens.

// After the introduction of dynamic, the access to the Value property and
// the conversion to Excel.Range are handled by the run-time COM binder.
dynamic excelApp = new Application();
excelApp.Cells[1, 1].Value = "Name";
Excel.Range range2010 = excelApp.Cells[1, 1];

Tags: .net, .net interview questions, csharp interview questions, dynamic, it interview questions with answers, programming interview questions

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded