tags:

views:

45

answers:

2

The title pretty much sums it up, it puts the following line of code in:

 <% foreach (var item in Model) { %>

As the View is auto-generated and uses reflection to work out exactly what item is, so you get intellisense and everything, I just wonder, why do they use var rather than the actual type?

A: 

I would consider it best practice to always use var. I think that is more readable. My guess is that microsoft feels the same way.

Mattias Jakobsson
I know many think this way, but I think var makes code harder to read. I run into code examples in books and blogs that use 'var' and there is no possible way to figure out what the types are. In a full program, I'll often have to consult the item definition. 'var' is *required* for anonymous types though.
kervin
@kervin, It is just my personal opinion. In the end it doesn't really matter. Just code the way that you feel comfortable with.
Mattias Jakobsson
+1  A: 

I like the var statement and consider it a great addition to the C# language. It is just one less place to worry about when I decide to change the type. I also try to follow the DRY principle, why repeat the type of the variable when it is already in the strongly typed view definition?

But I guess it is a matter of personal preference whether or not to use var.

Darin Dimitrov