var vs specified data type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

var vs specified data type

Hello all. What is the difference between using var to using the actual data type in case i know what data type i need (which we almost always know) var num = 3; int num = 3; Thanks in advance.

9th Jun 2017, 5:32 AM
Shahar Levy
Shahar Levy - avatar
21 Answers
+ 3
Guys do not relate c# var with var in other languages like, JavaScript, PHP, VB etc. They are totally different things. C# dynamic type is perhaps the closest you can get while comparing c# var with var in dynamic languages.
13th Jun 2017, 11:06 AM
NeutronStar
NeutronStar - avatar
+ 7
it is the same. However, using specific data types is way clearer. for example: var value = ServerResponse(); //you don't know what type is returned
9th Jun 2017, 6:56 AM
Edi Lipovac
Edi Lipovac - avatar
+ 2
No it does not. The IL code is the same. The compiler replace var with the actual real data type. No difference at the run time.
10th Jun 2017, 8:38 AM
Lukasz
+ 2
I've also wondered about that one! https://www.sololearn.com/discuss/437678/?ref=app
12th Jun 2017, 11:13 AM
Iwan
Iwan - avatar
+ 1
@Shahar Levy, The actual type of var variable is deduced at compile time, so there are no runtime overheads.
10th Jun 2017, 10:09 PM
NeutronStar
NeutronStar - avatar
+ 1
if we mentioned var the memory allocate dynamically. it is grate one. if we specified data type(int , float, string, double) then also no problem. choice is yours. in JavaScript, vb, php etc , we used that type of data types
11th Jun 2017, 5:26 AM
meherDev
meherDev - avatar
+ 1
var is explicit casting while mentioning datatype is implicit casting. In normal language, when you write var c# assigns the datatype based on assignment. This makes it useful when you don't know what the result of your right hand operation is going to be. If you know what result is going to be use datatype as it makes it more comprehensible to others and yourself.
11th Jun 2017, 6:48 AM
Kundan
Kundan - avatar
+ 1
If you use var as datatype the compiler does not recognize and warn you for any compile error. Also in code it is more obvious which datatype you use. So I prefer real datatype
11th Jun 2017, 7:10 AM
sneeze
sneeze - avatar
+ 1
I feel using actual data types speeds up execution time though it may not be noticeable
12th Jun 2017, 4:41 AM
Onyedikachi Nnadi
Onyedikachi Nnadi - avatar
+ 1
var keyword doen t add much . it s type is verified at compilation time . otherwise use dynamic key word
12th Jun 2017, 4:59 AM
Driss Boumlik
Driss Boumlik - avatar
+ 1
If you know the type of a variable, tell it to the compilator and don't make him lose time to choose the correct variable type.
12th Jun 2017, 9:44 AM
Kanaï
Kanaï - avatar
+ 1
@Onyedikachi, It has got nothing to do with speed since the actual datatype substitution happens at compile time not at runtime.
13th Jun 2017, 10:54 AM
NeutronStar
NeutronStar - avatar
0
Both are totally different. Specified DataTypes - > are the DataTypes used to allocate a particular predefined memory block with its nature. Use of specified DataTypes is recommended. Var - > when you don't know which type of memory you should allocate and at Runtime it decides the memory block by seeing its RHS. Example-> in angularjs & mvc we use var keyword because we don't know what type of data it could be. Thanks and Regards, Jai Patel
9th Jun 2017, 6:22 PM
Jai Verma
Jai Verma - avatar
0
It's basically the same thing some compilers specify the data type for you based on the value assigned to it...A char value would automatically initialize the variable as a char,same goes to float,int,array
10th Jun 2017, 2:54 AM
thomo
thomo - avatar
0
The question is, does using the var keyword cost additional resources? because the CPU needs to determine the data type.
10th Jun 2017, 4:16 AM
Shahar Levy
Shahar Levy - avatar
0
@sneeze what are u taking. in JavaScript we are frequently use var data type, in PHP we use $ sign. for giving us good experience
11th Jun 2017, 1:07 PM
meherDev
meherDev - avatar
0
I use C#. The question was about c#.
11th Jun 2017, 1:18 PM
sneeze
sneeze - avatar
0
both same
13th Jun 2017, 5:00 PM
Imran Alam
Imran Alam - avatar
0
@Shahar Levy, Why use var when one already knows the datatype one need? 1. For Refactoring - consider this, say you have a method that returns an integer then normally you would do like this int res = SomeMethod(); // specifying exact data type. Later on you realize that instead of returning int, returning string would be better option, so you would change the return type of the method from int to string. Now you have string SomeMethod() instead of int SomeMethod(). It may seems small change but did you considered the side/after effects of making this change ? I mean the integer version might be in use at 100's or 1000's of places in your code. If this be the case then you would have to replace int with string at each such place. This is not only time consuming but is also error prone resulting in a broken app. However if you had used var version var res = SomeMethod(); then the only place you need make changes are the file(s) that contains declaration and definition of the method SomeMethod. This is much more safer than making changes in large numbers of files. 2. Improved code readability - but for some people it is improved code readability at the cost of decreased understability. List<somethinglongtypename> v= new List<somethinglongtypename>(); // 1 Consider var version var = new List<somethinglongtypename>(); // 2 Clearly var version is more readable but since the type information is gone, it is harder to understand.
13th Jun 2017, 11:10 PM
NeutronStar
NeutronStar - avatar
0
how can l get runtime string input.For the purpose of checking alphabet ah vowel or content
14th Jun 2017, 5:55 PM
kathir vel
kathir vel - avatar