14

Possible Duplicate:
What's the point of the var keyword?
What advantages does using var have over the explicit type in C#?

I always see other people producing code like:

var smtp = new SmtpClient();

But why use var instead of SmtpClient in this case? I ALWAYS use

SmtpClient smtp = new SmtpClient();

Is var more efficient? Why use var instead of the actual variable type? Am I missing something?

Community
  • 1
  • 1
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
  • 1
    possible duplicate of [What advantages does using var have over the explicit type in C#?](http://stackoverflow.com/questions/3425966/what-advantages-does-using-var-have-over-the-explicit-type-in-c) see also [C# 'var' vs specific type performance](http://stackoverflow.com/questions/356846/c-sharp-var-vs-specific-type-performance) – Matt Ball May 13 '12 at 14:56
  • @MattBall Lol and **THAT's** a duplicate too – Liam McInroy May 13 '12 at 14:57

4 Answers4

31

Imagine this.

Dictionary<Dictionary<String, String>, String> items = new Dictionary<Dictionary<String, String>, String>();

var is useful for things like that.

var items = new Dictionary<Dictionary<String, String>, String>();

Much simpler. The point of var is the same as auto in C++ 11, the compiler knows the type so why must we repeat ourselves so much. I personally use var rarely, but only for lengthly declarations. It's just syntactic sugar.

David Anderson
  • 13,558
  • 5
  • 50
  • 76
21

No, it's not about efficency, it's only about code-writing style.

Considering that it's about a style, this rule could not be applied always.

Immagine following snipplet:

var myVar = Some3dPartFunction() , what is the type of myVar? It's not absolutely clear, and not good, from code-style point of view, to write in this way.

In short, choose appropriate writing in a clever way.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 3
    If you can't tell what function returns from its name, it's a badly named function. – Matěj Zábský May 13 '12 at 14:59
  • 2
    @MatějZábský: in a perfect world I would agree with you, but coding in the real office I **always** meet bad named functions, variables, not commented code, not tested code..whatever. You can not design a framework to fit needs of the perfect world.. – Tigran May 13 '12 at 15:01
  • @Tigran Of course, I also use specific type declaration where var would not be clear enough for some reason, but in general, I prefer var. You got my +1. – Matěj Zábský May 13 '12 at 15:21
5

No it isn't more efficient. It's the same thing of write explicitly the type. Infact it is good programming write the type of the variable instead of use var because it makes the code more readable.

So:

var smtp = new SmtpClient();

And:

SmtpClient smtp = new SmtpClient();

Are the same thing wrote in two different ways.

Omar
  • 16,329
  • 10
  • 48
  • 66
3

It's syntactic sugar:

var smtp = new SmtpClient(); 

is equal to:

SmtpClient smtp = new SmtpClient();

It's a new feature in C# 3.0, called type inference.

Mat
  • 202,337
  • 40
  • 393
  • 406
dfang
  • 1,366
  • 15
  • 41