Interviews5 answers
What is dynamic keyword ?
5 Answers
Dynamic Keyword is the run time keyword
Manoj Kumar DuraisamyPosted Jan 2, 2017, 8:53 PM EST
dynamic is the run time bounded so it will decide the type of value on run time. its dynamic typed variable
manisha vermaPosted Nov 14, 2016, 12:11 PM EST
Dynamic is a new type available in .NET to bypass compiler check during variable initialization and does it on run time. It is more flexible but dangerous and needs to be used more carefully. One cool feature of dynamic is to interact with other language code and execute them, e.g., in example PythonIron is used to interact with Python code and execute it. Also, dynamic can be used with ExpandoObject class available in .NET where you can declare properties and use them on run time. In ASP.NET MVC ViewBag is used to communicate data between Controller and Views which is a good example of dynamic. The simple example to run the Python code through dynamic is as follows: (Get IronPython from NuGet)
Hide Copy Code
public static void Main()
{
var pythonRuntime = Python.CreateRuntime();
dynamic pythonFile = pythonRuntime.UseFile("Test.py");
pythonFile.SayHellotoPython();
Console.ReadLine();
}
Where Python.py is:
Hide Copy Code
import sys;
def SayHellotoPython();
print "Hello I am Python"
Balaji PalaniPosted Jun 1, 2016, 10:54 PM EDT
No need to initialize dynamic variables at time of decalration.
var types needs to initialize at of time of declaration becuase its statically typed variable.
But dynamic is dynamically typed, so we can declare it and can initialize later,also the assigned value can change any time.
Explanation of dynamic
------------------------------------------
Introduced in C# 4.0
No need to initialize at the time of declaration.
Errors are caught at runtime
eg:
e.g., dynamic str;
str=”I am a string”; //Works fine and compiles
str=2; //Works fine and compiles
vipin kvPosted Mar 10, 2016, 3:59 AM EST
dynamic type is inferred at run-time and not the compile time.dynamic type variables need not be initialized when declared.
Anil Kumar MurmuPosted Jan 15, 2016, 1:47 AM EST



