Which one is better? "var" or "DataType"? [closed]
which one is better or maybe faster for declaring a variable?
var ds = new Class1();
OR
Class1 ds = new Class1();
I myself believe that second one should be faster coz compiler doesn't need to look for the type Class1
, but some addins like ReSharper
always notify me to change Class1
to var
.
They compile to the same IL, so neither will be faster. However, there can be a big difference in readability.
I tend to favour explicitly declaring the type of the variable, but use var
if any of the following cases applies:
- I'm calling a constructor and the type name is long (particularly with generics)
- I'm using anonymous types
- The type name is reasonably long and the initialization expression is long, but still very clear as to what type it will return
- I want to emphasize the purpose of the code instead of how it achieves its result
As with many issues of readability, there's a vast range of opinions about where to use var
- from "nowhere" to "everywhere". Note that you can change what ReSharper recommends for you in the options. (I seem to remember that by default it "recommends" both ways round - so really it's just making it easier for you to switch.)
A couple of answers have mentioned the number of keystrokes involved. I think this is the worst possible reason to favour var
. I'm rarely, if ever, bottlenecked on typing speed. My coding speed is far more heavily dependent on my understanding of the problem domain, and on how clearly I can imagine the solution. I would far rather enter 2000 keystrokes but end up with an elegant design which is actually represented in 100 characters than type a mere 500 keystrokes for a less-readable 500-character design. Think about the time spent reading the code rather than the mechanics of typing.
They both compile to the same thing. When using var
, you're simply letting the compiler infer the type at compile time. That may add a little bit of time to compilation (I haven't actually tested that part), but will have no impact at run-time.
The advantage of the former (which is what I prefer personally) is when you start to deal with long Type names:
var conn = new System.Data.SqlConnection(connString);
Is a lot less keystrokes and easier to read than:
System.Data.SqlConnection conn = new System.data.SqlConnection(connString);
In addition, without var
, using Anonymous Types would be nearly impossible:
var someObj = new { Name = "Test", Saying = "Hello World!" };
There are definitely times that I prefer to use the full type name instead of var
though. The most notable of which is when handling a return value of a method. If it is not clear from the name of the method what the type of the return value to be, I will explicitly define it. That reduces confusion in case anything changes down the road.
This is merely type inference at compile time, so there is no performance cost. The "performance gain" is that it is faster to write var
:)
As a rule I would favour leaning against var
when it isn't necessary (it is with anonymous types) and the type name isn't horribly complicated (Dictionary<int, List<Class.InnerClass<HashSet<SomeOtherClass>>>
or such).
Every time I hear about this suggestion, checking out ReSharper slides a bit further down my to-do list.
There is no performance difference -- the type of var is determined at compile time.
As to which one is better, it's a matter of preference. Personally, I prefer var if the code clearly identifies the type of var. Example:
// very much preferred
var o = new System.blah.blah.Class1()
However, if you have to think too much about the type, then like the type def:
// I don't like this much because the return type isn't obvious
var result = AMethodThatReturnsSomeKindOfValue()
Go with whatever is more readable. In your example, I think that the first example is more readable and has less duplication. If you don't have the class name on the same line (because you are initializing the variable to the result of a method call for example), then I would use the type name declaration style to make it explicit to whoever is reading the code.
精彩评论