What's the difference between dynamic(C# 4) and var?
I had read a ton of articles about that new keyword that will ship with C# v4,but I couldn't make out the difference between a "dynamic" and "var".
This article made me think about it,but I still can't see any difference.
Is it that you can use "var" only as a local variable,but dynamic as both local and global?
I'm sorry for my ignorance,but could you show some code without dynamic keyword and then show the same code with dynamic keyword?
var
is static typed - the compiler and runtime know the type - they just save you some typing... the following are 100% identical:
var s = "abc";
Console.WriteLine(s.Length);
and
string s = "abc";
Console.WriteLine(s.Length);
All that happened was that the compiler figured out that s
must be a string (from the initializer). In both cases, it knows (in the IL) that s.Length
means the (instance) string.Length
property.
dynamic
is a very different beast; it is most similar to object
, but with dynamic dispatch:
dynamic s = "abc";
Console.WriteLine(s.Length);
Here, s
is typed as dynamic . It doesn't know about string.Length
, because it doesn't know anything about s
at compile time. For example, the following would compile (but not run) too:
dynamic s = "abc";
Console.WriteLine(s.FlibbleBananaSnowball);
At runtime (only), it would check for the FlibbleBananaSnowball
property - fail to find it, and explode in a shower of sparks.
With dynamic
, properties / methods / operators / etc are resolved at runtime , based on the actual object. Very handy for talking to COM (which can have runtime-only properties), the DLR, or other dynamic systems, like javascript
.
Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed. This capability was added to the CLR in order to support dynamic languages like Ruby and Python.
I should add that this means that dynamic declarations are resolved at run-time, var declarations are resolved at compile-time.
I am going to explain difference between dynamic and var .
dynamic d1;
d1 = 1;
d1 = "http://mycodelogic.com";
This will work. compiler can re-create the type of dynamic variable.
first it create type as integer and after that compiler will recreate type as string
but in case of var
var v1; // Compiler will throw error because we have to initialized at the time of declaration
var v2 = 1; // Compiler will create v1 as **integer**
v2 = "Suneel Gupta"; // Compiler will throw error because, compiler will not recreate the type of variable
When using the ' var ' keyword, the type is decided by the compiler at compile time, whereas when using the ' dynamic ' keyword, the type is decided by the runtime.
' var ' keyword, a strongly implicitly typed local variable for which the compiler is able to determine the type from the initialization expression - very useful when doing LINQ programming.
Compiler doesn't have any information about the dynamic type of variable. so compiler will not show any intelligence .
compiler has all information about the stored value of var type so compiler will show intelligence.
dynamic type can be passed as function argument and function also can return object type
But
var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined. 链接地址: http://www.djcxy.com/p/95964.html
上一篇: 何时使用ORM(Sequel,Datamapper,AR等)与纯SQL进行查询
下一篇: 动态(C#4)和var之间有什么区别?