0
What's the difference between int num = 42 and var num = 42 ?
3 Answers
+ 9
var keyword is like: "come here compiler and decide for me what is the best variable for this value".
in your case it might not be different. In some cases for example in a LINQ query it is really helpful. Most of the time programmers use var keyword but sometimes they dont( forexample when they know clearly that the variable has to be a "whole number" they pick int so any cases it wants to pick up something else the compiler will say no.) Also as you dive deeper in c# sometimes you do not even know what type of variable will be the outcome of a session. Like when you get an answer from a server. I suggest you to use var keyword if only you know the exact type you will have assigned to the variable.
+ 2
The difference is that with the 'int num' declaration, the data type is explicit while with 'var num', it's implicit which means the compiler will determine the data type for the 'var num' declaration. ..
0
also if you will not have negative number assigned to the variable but the variable will go over 12049 the compiler might say okay this is an unsigned int and such. which can actually save memory