-3

Do var Impact performance

As currently, I am working on optimisation to speed up the execution process.

As My code has lot of declaration like following

var lstProducts = dbSet.ToList();

Now if I change it to strongly type as following,

List<Product> lstProducts = dbSet.ToList();

do it impact the execution time. or CPU cost/compilation time or such or it do not make any sense?

BJ Patel
  • 6,148
  • 11
  • 47
  • 81

1 Answers1

2

No, both alternatives compile to the same IL.

knocte
  • 16,941
  • 11
  • 79
  • 125
  • 1
    Performance should be just about the same, but setting the type makes use of the variable easier by seeing the type. By using var you first need to see what the method is actually returning. – JP Veldtman Feb 19 '17 at 17:06
  • @JPVeldtman ... at compile time! Which doesn't change execution speed at all. Note that when you explicitly name the type, the compiler *still* has to check the function return type *and* make sure it's assignable to the declared variable type. – Lucas Trzesniewski Feb 19 '17 at 17:08
  • Correct, that is why i said it just reads easier that you know what type it is returning, than "guessing" by just looking and not hovering or inspecting. Especially when someone else looks at your code. Like var a = getId() method can return int, long etc. but saying int a = getID() you know for sure you are expecting an Int wby just looking at the code – JP Veldtman Feb 19 '17 at 17:16