Difference between dynamic and var keyword .net 4.0

Posted on May 19 2010 by Sachin Jain
In .net 4.0 dynamic keyword has been introduced. This allows you to define the object without specifying its type. So let say we have following string :-
string s = “sachin”;
We can define another variable without specifying the type
dynamic d = s;
Now this can also be done through var keyword so whats the difference?
The difference is dynamic use run time binding and var use compile time binding
for example if we do this:-
var c = s.get();
It will show compile time error as s has no method like get();
but if we do
dynamic d = s.get();
It will compile successfully and shows error on runtime.
Also dynamic variable allow use of dynamic languages like ruby, python etc. and also simplifies the reflection.
VN:F [1.9.0_1079]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.0_1079]
Rating: +1 (from 1 vote)

One Response to “Difference between dynamic and var keyword .net 4.0”

  1. panakj says:

    good…keep it up…

    UN:F [1.9.0_1079]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.9.0_1079]
    Rating: 0 (from 0 votes)

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Powered by Sachin Jain

Powered by Olark