What is Dynamic keyword in C#?
The dynamic keyword and the Dynamic Language Run time (DLR) are major new features introduced in C# 4 and the Microsoft .NET Framework 4. This article aims at only providing details about dynamic keyword and is not going to cover DLR.
Static typing and dynamic typing are the concerns of programming language design. “Typing” over here refers to the data type. It might sound obvious to many, but not for a few. Statically typed programming languages require you to declare the data types of your variables before you use them, while dynamically typed languages do not. Please do not confuse statically typed with static keyword in C#
Example: (C#)
int num;
num=1;Example: (PHP)
num=1
A statically typed language is validated for syntax and errors during compilation of code. Dynamically typed languages like PHP, JavaScript is validated for syntax and other error only during run time. C# originally was created as a purely static language, addition of dynamic keyword and DLR in .NET Framework brought dynamic language capabilities to C#.
When you use the dynamic keyword you tell the compiler to turn off compile-time checking.
It’s possible to assign objects of different types to a variable declared as dynamic. DLR analysis provider uses reflection for C# to perform the run time analysis. For C# with Reflection, you do not get any caching behavior, which means that operations are generally slower, but there is no memory cost for maintaining the cache and every operation is roughly the same cost. With the DLR, the first operation is very slow as it does a huge amount of analysis, but the analysis is cached and reused. That consumes memory, in exchange for increased speed in subsequent calls in some scenarios
Pros of using dynamic
- The code is much faster and easier to write using dynamic keyword.
- Improved interoperability with COM/ATL components. (We would not go into much details over here. COM is a standard introduced by Microsoft in late 1993, used to enable interprocess communication object in large range of programming languages)
Cons of using dynamic
- You don’t get compiler errors and have to use unit testing and other techniques to ensure the correct behavior of your application.
- The absence of compile-time type checking leads to the absence of IntelliSense as well. Because the C# compiler doesn’t know the type of the object, it can’t enumerate its properties and methods.
- Use of dynamic keyword will slightly affects application performance as DLR analysis is cached, unlike reflection.
Following code sample will help you understand how use of dynamic keyword makes your code simple to understand
If you are unable to view the above snippet, you can try viewing the code on Dot Net Fiddle: https://dotnetfiddle.net/Bw9QKL
Hope this article gave you some idea about dynamic keyword in C#. Please feel free to provide your suggestions on improving the article. Stay tuned for more!
Need help in understanding any topic? Let us know by filling up this form http://bit.ly/RequestCourse