5
Reply

What is dynamic keyword ?

Lalit Raghav

Lalit Raghav

9y
1.2k
0
Reply

    Dynamic Keyword is the run time keyword

    dynamic is the run time bounded so it will decide the type of value on run time. its dynamic typed variable

    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 Codepublic 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"

    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 compilesstr=2; //Works fine and compiles

    dynamic type is inferred at run-time and not the compile time.dynamic type variables need not be initialized when declared.