pookie
pookie

Reputation: 4142

Is it more efficient to use explicit variables?

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?

Upvotes: 3

Views: 122

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 4

Related Questions