Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using var benefits
Message
From
27/05/2011 11:09:10
 
 
To
27/05/2011 10:15:34
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
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:
01512061
Views:
65
>Hi all,
>
>Is there any performance benefits to using or not using var to declare objects?
>
>Is this better in some way (or not)
>
>DBRange range = new DBRange();
>
>over this:
>var range = new DBRange();
>
>Thanks

Hi Tim,

There are no performance hits, since the type is inferred by the compiler ( at compile time)

Using var is fine imo, but you can get a surprise now and then. Since it's the compiler that assigns the type, it may not be the type you intended to use.

It may assign a type where you wanted to use an interface. It may assign an interface where you wanted to use another interface


With LINQ, it's best to use var. [ More Effective C# - item 30 ]
(1) You do not always know what type it will return - and do you care ?
(2) Some queries return an IEnumerable(T) where others return an IQueryable(T). If you force an IQueryable(T) into an IEnumerable(T) , you may miss out any enhancements provided by the IQueryProvider

[ item 43 ]
IEnumerable(T) = LINQ to objects
IQueryable(T) = LINQ to SQL
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform