.Net 4.0 introduces the Dynamic Keyword in C#.
Its great feature. The intention is to use it when interacting with dynamically typed languages like Python (IronPython in .Net framework), Ruby (IronRuby in .Net framework), JScript and so on.
Dynamically Typed Vs Statically typed
Statically Type Languages
|
Dynamically Typed Languages
|
The Type of expressions / variables declared is known at compile time
|
The Type of expressions / variables declared is not known at Compile time, but type checking is delayed until runtime
|
Any type mismatch is caught at compile time. Guarantees type safety.
|
Type mismatch is caught at run time.
|
Semantic Analysis tasks, like overload resolution occur at compile time and are added into the assembly.
|
Semantic analysis tasks are delayed until run time
|
One such example of dynamic Type is "var". We use var in javascript, Python and so on.
The type of variable of var type, is decided by the value assigned to it or rather the first value assigned to it.
Well System.Object Type does the same job. If you read the msdn definition for Dynamic Keyword, it also pretty much does the same thing. So what's the difference?
Lets see.
Lets see.
Generics Vs Dynamic
We could add generics to this list, but it gets ruled out, as in the case of generics, the concept itself forces compile time type check. The compiler ensures type safety.
Var Vs Dynamic
In dynamically typed you have weak and strong dynamically typed.
In case of Python, say you use "var". The runtime determines the type through its first assignment.
Eg.
var obj = 4935; ///The type would be Int
obj = "abcdefg"; ///This will throw an error. As the type is already determined as int.
This is strong dynamically typed.
In case of languages like Javascript, you can have the above case is valid. Such languages are weakly typed.
In case of languages like Javascript, you can have the above case is valid. Such languages are weakly typed.
In C#, type check for var is done at compile time. So it is opposite to "dynamic" keyword.
In the case of the "dynamic" keyword, it uses both the weakly typed and strongly typed concepts.
dynamic dynamicExample = 20;
dynamicExample = dynamicExample + "ABC"; ///Will concatenate
Output:
20ABC
-------------------------------------------------------------------------------
dynamic dynamicExample = 20;
dynamicExample = dynamicExample + 20; ///Will do a math addition
Output:
40
-------------------------------------------------------------------------------
dynamic dynamicExample = 20;
dynamicExample = dynamicExample / "ABC"; ///Will compile, but will throw a runtime exception
Output:
Error: Operator '/' cannot be applied to operands of type 'int' and 'string'
-------------------------------------------------------------------------------
dynamic dynamicExample = 20;
dynamicExample = dynamicExample.DoSomething() / "ABC"; ///Note that DoSomething() does not exist. But it will compile. This is because, when the compiler reads the dynamic keyword it delays any type check to runtime.
Output:
Error
Dynamic is intended to be used when interacting with Dynamically typed languages like Python (IronPython in .net 4.0), Ruby (IronRuby in .Net 4.0) and the rest of the dynamically typed languages added to framework, COM Objects.
Example with Excel operations
System.Object Vs Dynamic
In the case of dynamic keyword
Now lets try it using System.Object Type
Performance
- Static types would perform better, obviously because the compiler has done type checking.
- The dynamic keyword, does make your life simple and code simple, but I do not agree it performs better. It has an overhead to perform the compile time checks that were delayed to runtime. The decision depends on the context of use.