Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using var benefits
Message
De
27/05/2011 12:10:18
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
27/05/2011 11:09:10
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:
01512065
Vues:
44
Excellent Gregory,
Thanks!

>>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
Timothy Bryan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform