1

Possible Duplicate:
C# 'var' vs specific type performance

Are there any performance costs (in terms of type conversion, etc.) if I write the line

SqlConnection c = new SqlConnection(connectionString))

as

var c = new SqlConnection(connectionString))
Community
  • 1
  • 1
Silverlight Student
  • 3,968
  • 10
  • 37
  • 53
  • 6
    No type conversion occurs. `var` is syntactic sugar, the compiler infers the correct type. – Oded Jul 25 '11 at 20:12
  • 4
    Come on, this has been asked a dozen times on SO alone (see http://stackoverflow.com/questions/356846/c-var-vs-specific-type-performance and its `linked` section for some). Is this kind of thing really that hard to google or infer from the easily-googleable descriptions of the feature? –  Jul 25 '11 at 20:13
  • There are also times when there is no static type, creating or projecting into anonymous types for instance. You have to use var as the type then. – asawyer Jul 25 '11 at 20:14
  • And can people please find other excuses to ask questions than "performance costs"? – BoltClock Jul 25 '11 at 20:16
  • @delnan: sorry, I guess i am too lazy today :) – Silverlight Student Jul 25 '11 at 20:16
  • The difference should be explained by now. Note that using `var` instead of types makes your code much more refactoring friendly, too :) – Zebi Jul 25 '11 at 20:30
  • @BoltClock: Not sure what you mean by finding excuses. I just came across this code that was using var and start thinking about any potential side effects this may have. I understand that this question may been asked before but you just don't answer if you don't like it; Is it too big a deal? – Silverlight Student Jul 25 '11 at 20:30

3 Answers3

7

No. The compiled IL is identical.

The only potential side effect is in the case of inheritance, if you're variable definition is a base class, and you instantiate a subclass. If you do:

 BaseClass item = new DerivedClass();

This will potentially act differently than:

 var item = new DerivedClass();

This is because the second compiles to:

 DerivedClass item = new DerivedClass();

In most cases, it should behave identically (due to the Liskov substitution principle). However, if DerivedClass uses method hiding it is possible to have a change in behavior.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • so then why any develoepr should use "var" when using "SqlConnection" make code more readable (IMHO)? – Silverlight Student Jul 25 '11 at 20:12
  • See http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c – BoltClock Jul 25 '11 at 20:13
  • 1
    @Silverlight Student: Most people feel that the first makes the code more readable - especially in the case of method chains used in features like LINQ. It also allows for anonymous types to be used. – Reed Copsey Jul 25 '11 at 20:14
  • Because he thinks the redundancy makes it less readable. But there are existing questions on this. It comes down to a matter of style, and those a personal preference. And there are some scenarios (anonymous types) where `var` is necessary since you can't name the type explicitly. – CodesInChaos Jul 25 '11 at 20:14
  • @Silverlight Student: It's a matter of taste--but it can shorten up a line of code considerably if the type name is long (especially when using generics) and makes a line more readable in certain cases by not repeating the type name. It also comes in handy when writing code in books or forums to shorten up otherwise lenghy lines of code so that the reader can concentrate on the relevant part of the code. – Matt Smith Jul 25 '11 at 20:18
1

No. The compiler knows at compile time what var should be (the return of new SqlConnection is, in fact, SqlConnection. When the compiler knows the type of the right hand side, you can use var.

This has no runtime performance implications

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
0

I believe var is processed at compile time, so it is simply a shortcut for writing the code. This means that the compiled version is identical.

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33