What is the difference between my codes?
This question already has an answer here:
As suggested by Brad Smith http://www.brad-smith.info/blog/archives/336 :-
There seems to be a tendency for some programmers to use var for every variable declaration. Sure, the language doesn't stop you from doing this and, indeed, MSDN admits that this is a “syntactic convenience”… But it also warns quite strongly that:
the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.
Implicitly Typed Local Variables (C# Programming Guide), MSDN
I discovered recently that the commonly-used tool ReSharper practically mandates liberal use of var. Frankly, this isn't helping the situation. There are some developers who try to argue the stance that var somehow improves readability and broader coding practices, such as this article:
By using var, you are forcing yourself to think more about how you name methods and variables, instead of relying on the type system to improve readability, something that is more an implementation detail…
var improves readability, Hadi Hariri
I agree with the premise of the quote above, but not with the end result. On the contrary, the overuse and misuse of var can lead to some very bad habits…
Let's look at the argument against the widespread use of var (and for its sparing, correct use):
Implicitly-typed variables lose descriptiveness
The type name provides an extra layer of description in a local variable declaration:
// let's say we have a static method called GetContacts()
// that returns System.Data.DataTable
var individuals = GetContacts(ContactTypes.Individuals);
// how is it clear to the reader that I can do this?
return individuals.Compute("MAX(Age)", String.Empty);
My variable name above is perfectly descriptive; it differentiates between any other variables populated using GetContacts() and indeed other variables of type DataTable. When I operate on the variable, I know that it's the individual contacts that i'm referring to, and that anything I derive from them will be of that context. However, without specifying the type name in the declaration, I lose the descriptiveness it provides…
// a more descriptive declaration
DataTable individuals = GetContacts(ContactTypes.Individuals)
When I come to revisit this body of code, i'll know not only what the variable represents conceptually, but also its representation in terms of structure and usage; something lacking from the previous example.
No difference, by typing var instead of the data type that you are using, you just make the compiler look and set the data type himself. It makes the code a little shorter but in my opinion its better to write the data types full name instead of var.
It is same. But, the only difference is that, compiler determines the type of the variable.
How C# compiler does know the type of the variable?
The C# compiler infers the type of the variable from the right-hand-side expression . For example, the type for the students
is inferred from the type of the right-hand-side expression new List<Student>()
, which makes students
a type of List<Student>()
.
上一篇: 'var'的目的是什么?
下一篇: 我的代码有什么区别?