Dynamic type has been added to C# since C# 4.0 (.NET 4.5) and its main purpose is to bypass the static type checks and add more flexibility to the language.
Static vs Dynamic Languages
As you might be aware, the software development languages are divided into two major categories: static languages and dynamic languages. The main difference between a static and a dynamic language is how it handles its types (or doesn’t).
in the static languages variables are resolved during “compile time” and in the dynamic in the “runtime”.
What does this really mean though?
In a static language, a variable is assigned its type when the project is compiled.
In a dynamic language, a variable can change it’s type several times while the application is already running.
Some examples of static and dynamic languages:
Static: C , C# , F# , C++ , Java , Go
Dynamic: Javascript , Python , Ruby , PHP , Perl , Objective-C
Advantages of Static and Dynamic Languages
Static languages are generally considered to be faster because they resolve their types during the compilation phase. This helps improve the application performance and optimization. Excluding machine language and Assembly, fastest high-level languages are probably C and C++. No other languages can still match the speed of these two because of their memory management capabilities.
Besides being fast, static languages are also being fast to fail. You’ll find a lot of bugs even before the application has started because of the compiler checks. This means fewer bugs once the application is up and running.
On the other hand dynamic languages, while being slower, are much easier to write. And you can write them faster without having to think about which type to use or how to initialize it.
What is Dynamic Type in C#
So we already mentioned that C# is a statically typed language. So what does a dynamic type has to do with C#?
The dynamic type has been added to C# since version 4 as because of the need to improve interoperability with COM (Component Object Model) and other dynamic languages. While that can be achieved with reflection, dynamic provides a natural and more intuitive way to implement the same code.
Dynamic type in C# is instantiated by using the dynamic keyword and it can be assigned any other type.
Why Should We Use the Dynamic Type
By now, you probably have some ideas on where you can use dynamic type. But let’s go through some common scenarios in which dynamic could potentially improve our applications and make our lives as developers a bit easier.
First of all, let’s make it clear that dynamic is not a silver bullet. We shouldn’t use it just because we can.
While it has its benefits, dynamic objects are harder to work with while writing code, since we don’t have and Intelligence for them due to the nature of dynamic type. On the other hand, if have a need to implement dynamic type everywhere, we are probably using a wrong type of language. Dynamic languages are better suited for those kinds of cases.
So what are the common cases to apply dynamic to:
- Communicating with other dynamic languages
- Simplifying responses from API calls when we don’t know what type of object to expect (or we don’t care)
- Creating libraries that can be used between languages
- Making generic solutions when speed isn’t the main concern
- Replacing and simplifying reflection code
Why we shouldn’t use dynamic all the time because:
- It’s slower than statically typed code
- Increases a chance to get runtime exceptions
- Decreases code readability in some cases, and working with it is a bit harder due to the lack of IntelliSense
Reference: https://www.linkedin.com
0 comments:
Post a Comment