0

I usually create a object like these:

List<Student> students= new List<Student>();

but when I install the resharper in vs2012,it suggest me to create a object like these:

var students= new List<Student>();

I wonder whether they have the same effect.I think they have the same effect.Will it be better when I use var

svick
  • 236,525
  • 50
  • 385
  • 514
flower
  • 2,212
  • 3
  • 29
  • 44

5 Answers5

3

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>().

Community
  • 1
  • 1
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
3

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.

Lynx
  • 506
  • 2
  • 10
  • according to Neel's comment,it seems that `var improves readability` – flower May 03 '14 at 09:54
  • 1
    I prefer seeing a variable defined as its data type and not as var. Like for instance by looking at a class you see a private field called var people. You do not know what is this people and you must take your time to search only to see what's the type of this variable which in my opinion is a waste of time. Everyone has a different opinion. – Lynx May 03 '14 at 09:57
3

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.

svick
  • 236,525
  • 50
  • 385
  • 514
Neel
  • 11,625
  • 3
  • 43
  • 61
  • 1
    Please make sure you make your entire post a quote by formatting it appropriately if you have taken it from somewhere else in its entirety. If not, users might end up flagging it. – Bart May 03 '14 at 09:42
  • I didn't know it thanx for information and still I got downvote even though I have specified the name of author, the link and everything in quote @Bart – Neel May 03 '14 at 09:46
  • Myeah, I can't speak for the downvoter. So I can't help you there unfortunately. But just be careful when quoting. If there is any hint of you claiming it to be your own, that could spell trouble. So always provide proper attribution. – Bart May 03 '14 at 09:48
  • ok got it @Bart I will be carefull – Neel May 03 '14 at 10:02
  • 2
    I don't see how does this answer the question: it doesn't actually explain what `var` does, and it doesn't even talk about the case in the question, where the type is spelled twice, so it's clear even with `var`. – svick May 03 '14 at 16:52
2

var is just a syntactic sugar. It aliases any type. In your case, there is no difference between them. They both produce same Intermediate Language code.

Using var is just saying: "Hey compiler, just determine the type of this variable"

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

var just infers the type. You can use it in declarations like this when the resulting type is pretty obvious - it makes the code easier to read but doesn't make a difference to the compiler.

See also:

Community
  • 1
  • 1
Gigi
  • 28,163
  • 29
  • 106
  • 188
  • +1 for easier to read. Sadly it's arguably less easy to maintain when you come back to it in 6 months time. – ClickRick May 03 '14 at 09:53
  • You just have to use it wisely. Simple declarations as in the question are good candidates for var usage. When you have tons of LINQ queries that use var, yes, it's a bit harder to figure things out. – Gigi May 03 '14 at 09:54