+ 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.
21 odpowiedzi
+ 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.
+ 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
+ 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.
+ 2
I've also wondered about that one!
https://www.sololearn.com/discuss/437678/?ref=app
+ 1
@Shahar Levy,
The actual type of var variable is deduced at compile time, so there are no runtime overheads.
+ 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
+ 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.
+ 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
+ 1
I feel using actual data types speeds up execution time though it may not be noticeable
+ 1
var keyword doen t add much . it s type is verified at compilation time . otherwise use dynamic key word
+ 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.
+ 1
@Onyedikachi,
It has got nothing to do with speed since the actual datatype substitution happens at compile time not at runtime.
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
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
0
The question is, does using the var keyword cost additional resources? because the CPU needs to determine the data type.
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
0
I use C#. The question was about c#.
0
both same
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.
0
how can l get runtime string input.For the purpose of checking alphabet ah vowel or content