Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using var benefits
Message
De
27/05/2011 11:09:10
 
 
À
27/05/2011 10:15:34
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows XP SP2
Divers
Thread ID:
01512055
Message ID:
01512061
Vues:
66
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform