dynamic vs var
A while ago, on a beautiful evening of a wonderful weekend I had a mug of beer in my hand and a thought striked, what’s new in C# 4.0. I searched on google and started to look into the results and suddenly I stopped to get a hang of the term ‘dynamic’, well obviously I googled again and burrowed deeper into it. After a couple of minutes (read hours) I got aquainted to the idea of ‘dynamic’ but could not understand its need as we already had ‘var’ (thanks to C# 3.0). This made me anxious and I started to look out for the need of ‘dynamic’ and the difference between ‘var and ‘dynamic’. Following are my findings
‘var’ was introduced in c# 3.0 and steadily got popular among geeks. Variable declared with var are implicitly but statically typed. By statically typed I mean that the type of variable declared is decided by compiler at compile time. So to let compiler decide what type of variable it is, as we need to initailize it at declaration. i.e.
var myName ; //Here compiler will show error as we didn’t initialize it and compiler is not able to decide its type
var myName = “XYZ” ; // So here compiler is happy as compiler know that it’s string type
myName = 45; //Compiler wil show error as myName is already string type and now i am assigning and integer to it
So by following example it’s clear that once compiler decided that a variable is string type then that variable will always be string type in it’s scope.
By doing it statically type the errors are caught at compile time and we also have the benifit of intellisense as compiler already know it’s type. i.e.
Console.WriteLine(myName.BeerWasGreat()); //This will show error on compile time as string doesn’t have any ‘BeerWasGreat’ method.
By reading all this about ‘var’ a thought can roll in any time i.e. Why use var when we know it’s type ie. String, int etc. Let me clarify
Where to use var
var is a splendid idea to help implement a key principle of good programming: DRY, i.e., Don’t Repeat Yourself.
VeryComplicatedType x = new VeryComplicatedType();
is bad coding, because it repeats VeryComplicatedType, and the effects are all negative: more verbose and boilerplatey code, less readability, silly “makework” for both the reader and the writer of the code. Because of all this, I count var as a very useful enhancement in C# 3.0.
So the above should be done as
var x = new VeryComplicatedType();
var is also useful in making anonymous type and here i am not going in detail of it. So for now that’s it for var and now move to dynamic
Dynamic has been introduced in C# 4.0. Variables declared as dynamic are dynamic type. By dynamic type I mean that type of variable declared is decided by compiler at run time. So there is no need to initialize the dynamic type at declaration time i.e.
dynamic myName; //This will work fine but need to assign later before running otherwise it will ` show error
myName = “XYZ”; //This will work fine and myName is a String
myName = 45 ; //Again this will work fine and now myName is integer
So by example it is quite clear that type of variable is decided on run time based on the last assigned object.
As type of dynamic variables is decided on run time so we are not able to use intellisense and also errors come up on run time rather than compile time i.e.
Console.WriteLine(myName.BeerWasGreat()); //This will show error on run time.
Where to use dynamic
A Last Thought
If you want to know more about dynamic and where to use it please go through the following
If you like this article and want to write me back to discuss over this furthur, you are welcome to leave comments or you can write me on my Email Id email@example.com.
Don’t forget to like and share..:)