Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using var benefits
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01512055
Message ID:
01512143
Views:
47
>>
>>I don't think there is any performance difference; however, my preference is to use var only in Linq....which is what is was designed for since linq queries change the typing..
>
>Sorry to be nitpicky here, but people could easily misunderstand the, "linq queries change the typing" comment and what var does. LINQ queries return an anonymous type - at compile time the compiler generates a type with a unique name. It has access to this name/type, but we don't (so we wouldn't be able to put the correct type in), hence the term "anonymous type". Since we don't have access to the correct type name they had to come up with some way of handling this - "var" which basically defers the type to the compiler - it will fill in "var" with the correct type at compile time (not at runtime).

Sorry to be nitpicky here, but (g):

Linq queries only return anonymous types if the query creates anonymous types. e.g where People is List of Person:
var y = from p in People select new { p.Name, p.Age };  //anonymous type
var v = from p in People  select p;    //not anonymous ( IEnumerable of Person )
Previous
Reply
Map
View

Click here to load this message in the networking platform