3

This question relates to C#.

Which is more efficient to use?

  1. int number = 5;

or:

  1. var number = 5;

My thinking is that there should be no difference in performance or memory during runtime, but compile time would be longer for option 2.

Is that correct? Are there any negative aspects of using var?

pookie
  • 3,796
  • 6
  • 49
  • 105
  • IMHO you should following best practices (in this case using `var` keyword), which becomes you code more readable. Also lets say you change right hand side of assignment, in first case you also have to change (potentially) type declaration, which is not a case in second. – tchelidze Jan 30 '16 at 20:38
  • According to http://stackoverflow.com/questions/356846/will-using-var-affect-performance, there is no difference. – Anopob Jan 30 '16 at 20:41
  • 1
    Some information about the var keyword [here](http://stackoverflow.com/questions/4307467/what-does-var-mean-in-c) and [here](http://stackoverflow.com/questions/209199/whats-the-point-of-the-var-keyword) – Marco Z. Jan 30 '16 at 20:44
  • 1
    compiler can do that thousand times before you blink. so dont worry. just pick the one which you feel better and is more readable. – M.kazem Akhgary Jan 30 '16 at 20:44
  • Not in this case, but you can't really use `var` like this for `short` or `byte`. – Sami Kuhmonen Jan 30 '16 at 20:49
  • @SamiKuhmonen Why not? I mean, what would be wrong with `var bytes = Encoding.Default.GetBytes(sometext);` ? – pookie Jan 30 '16 at 20:51
  • @pookie I mean with just `5` you have to define the type if you want it to be `short`, like `short a = 5;` – Sami Kuhmonen Jan 30 '16 at 20:53
  • @SamiKuhmonen Got you. That makes sense, since `var a = 5;` would be interpreted as an `int`, I assume. – pookie Jan 30 '16 at 20:55
  • 1
    Just one opinion here but I tend to prefer typing `int` in instances where the right-hand is a method call or a property getter because `var` makes it less clear what the method returns without looking at the method's definition. Example: `int number = GetFoo();` vs `var number = GetFoo();` – blins Jan 30 '16 at 20:55
  • @blins Good point, Thanks. – pookie Jan 30 '16 at 20:55
  • @pookie Yes. You can define literals for int, long, decimal etc and use var, but some types can't be defined that way – Sami Kuhmonen Jan 30 '16 at 20:56

1 Answers1

4

Which is more efficient to use?

int number = 5;

or:

var number = 5;

Both compile to the absolutely same IL, so there will be no difference at runtime. And if you are worried about a difference at compile-time for using var, I think that you'd better concentrate on more important aspects of your code and product and just don't waste your time in such thinking and focus on the more important stuff - which is writing readable code.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • In both cases, the compiler *must* evaluate the expression type on the right-hand side either way, to check if it matches the type defined on the left side of the `=`, or if an implicit conversion exists. Guess what... no check is ever needed for `var` :) I won't microbenchmark the compiler, but OP's supposition that `int` is faster to compile could be moot. – Lucas Trzesniewski Jan 30 '16 at 20:43
  • "no check is ever needed for `var`" - wow, that is interesting, Thank you, – pookie Jan 30 '16 at 20:46
  • Agreed, that's micro-benchmarking that one shouldn't ever be doing but rather concentrate on writing readable code and one that's optimized at runtime. – Darin Dimitrov Jan 30 '16 at 20:46