Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using var benefits
Message
 
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:
01512142
Views:
58
The benefit of var is that it makes code more portable. If you don't explicitly type something and you later end up changing a type name or new type altogether but keep the interface the same the code still works without further changes to your code or refactoring.

Under the covers the compiler does your work for you. It's a minor thing and I used to think it was better to use explicit types, but it saves a few short brain cycles and typing to do something like this:
var items = new List<ChmImportItem>();
instead of the duplication that is:
List<ChmImportItem> items = new List<ChmImportItem>();
No less clear what's happening but a bit less typing.

+++ Rick ---

>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
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform