>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