2

Why when creating object it is preferred to use var keyword than real type?

var strList  = new List<string>(); //good?
List<string> strList  = new List<string>(); //why bad?

Question is not just about List<T>.

nawfal
  • 70,104
  • 56
  • 326
  • 368
GraphicsToBe
  • 115
  • 4
  • 9
    There is no "good" or "bad". It's just a personal preference. The usage of the var keyword is just for convenience. The compiler will translate it into the actual type so there's no performance issue at runtime either. – Jensen Mar 18 '13 at 20:46
  • 1
    Actually it's a matter of preference. The first annoys the heck out of me ESPECIALLY when its the return value of a function... – Jesus Ramos Mar 18 '13 at 20:46
  • See also this SO question and the associated duplicates - http://stackoverflow.com/questions/3967993/pros-cons-of-var-datatype-in-net-3-5/3968030#3968030 – pstrjds Mar 18 '13 at 20:49
  • both will perform in equivalent manner according to me. – LearningAsp Mar 18 '13 at 20:53
  • 1
    possible duplicate of [Use of var keyword in C#](http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp) – Omar Mar 18 '13 at 20:54

4 Answers4

2

The var keyword is great for hiding noise. When you have a statement like

List<string> strList  = new List<string>();

you can be certain that strList is of type List. Typing the type twice is a waste of your time. If you decide to change the type later, you have to change it in both places.

My preference is to always use var when specifying the type is already obviously specified nearby. I often use var for any instance variable when the type is not of significant importance.

Eric
  • 18,512
  • 4
  • 29
  • 34
2

It is not that var is good and the other one is bad. It's kind of personal preference.

If you want me to be more specific, I think you should use var only when the type is completely evident, such as:

var employee = new Employee();

On the other side it's probably a horrible idea to do:

var e = GetEmployees();

Because it's hard to tell whether if e is an Employee object, or a List, array, etc.

Also is probably right to use it when it saves you some effort while not diminishing readability:

ReallyLongClassName someidentifier = new ReallyLongClassName();

VS

var explanatoryIdentifier = new ReallyLongClassName();

But at last, it comes down to be personal preference, just don't ever save you some characters if you are damaging readability.

1

It's just a good practice use List<string> strList = new List<string>(); (explicit typing). It makes the code more readable, but they are equivalent.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
Omar
  • 16,329
  • 10
  • 48
  • 66
  • 1
    I'd say "*readable*" in this case is a bit too subjective for one to truly be better than the other, but it does definitely make it more "*explicit*" as to the intent. – Jonathan Lonowski Mar 18 '13 at 20:48
  • When you have `List` it's more subjective is it better or worse, but when you have `Dictonary>>` or something similar (like LINQu result) it's starting make sense. It's also easier, when your having a method, and you will change the return type (for example to interface) - you don't have to change it in many places in your code. In constructors - it only reduces the number of characters you need to type. – Marek Kembrowski Mar 18 '13 at 20:53
  • Sometimes I wonder if it would have been useful for them to allow syntax like `List strList = new();` to keep the type on the left where it's most familiar to C# programmers. – Matthew Watson Mar 18 '13 at 21:05
  • 1
    @Downvoters care to comment? – Omar Mar 19 '13 at 16:51
1

The argument for why var is good is that it removes the duplication of declaring the type. Meaning if you want to modify the type that strList (which is a bad name) is, you do it in one spot.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156