8

At first I thought something like:

var aName=getAllSomethings();

Is very unreadable, and so I'll use dynamic typing just when there's no room for confusion such as:

AClassName aName = new AClassName();

Here,

var aName=new AClassName();

seems readable. But than I read (here) that dynamic typing also comes with a price in performance.
I tried reading all the other posts in that link to understand where I should use dynamic typing, but couldn't come up with even one good reason. Should I just wait for when I'll tell myself - "This can only be solved with dynamic typing" ? Or are there better (practical) reasons for using it?

Thanks.

Edit: My mistake (-: will close this question ASAP.

Community
  • 1
  • 1
Oren A
  • 5,870
  • 6
  • 43
  • 64
  • 2
    But just think about how much you learned! ;) – Robaticus Sep 05 '10 at 21:21
  • 1
    @Robaticus: +1 on that. This is why I stubbornly claim that the only stupid questions are those that are not asked, leading to people not learning. This question helps clearing out a misunderstanding that I believe is quite common, so it definitely serves a purpose. – Fredrik Mörk Sep 05 '10 at 21:23
  • 2
    No need to close it, I think it's a fairly common question. Other people can still benefit from the information. – Rei Miyasaka Sep 05 '10 at 21:24
  • @You guys: Thanks for your support (-: And I did learn a lot (-: – Oren A Sep 05 '10 at 21:29
  • possible duplicate of [Will using 'var' affect performance?](http://stackoverflow.com/questions/356846/will-using-var-affect-performance) – nawfal Jul 15 '14 at 14:49

6 Answers6

26

var isn't dynamic typing. It's just that the type of aName is inferred by the compiler.

Your example is still entirely statically typed, and has no performance penalty. Your code is compiled into exactly the same IL as it would be with an explicit type name.

Now in C# 4, dynamic typing does exist, but it would be written as:

dynamic aName = new AClassName();

My personal belief is that dynamic typing will be relatively rarely useful in C# 4 - basically when you're dealing with data which is already only known dynamically, e.g. reflection, or navigating XML.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

This is not dynamic typing. The var declares a static type known at compile time and it will be absolutely equivalent to declaring the type name. The compiler will infer the type and replace it in the resulting assembly.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
5

Using the var keyword isn't dynamic typing. The var keyword is handled by the compiler. The variable gets statically typed based on the first assignment to the variable.

The type of:

 var value = new StringBuilder();

Is StringBuilder. There's no way to change that type (which is what dynamic typing would allow).

What Jon Skeet is referring to in his blog is the dynamic keyword. That is a whole other beast.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

That isn't dynamic typing. Dynamic typing is through the "dynamic" type (oddly enough). Using var has no overhead at all, it is a coding shorthand that still produces strongly typed variables.

Robaticus
  • 22,857
  • 5
  • 54
  • 63
2

You are misunderstaninf why dynamic typing is.

"var" uses Type Inference to deduce at compile time the type of the variable. It's mechanism is fairly straighforward, and has absolutely no performance impact. A variable declared with "var" can still only be one exact type (or derivations of that type).

Dynamic variables allow a variable to hold any type at runtime, and still perform operations on that object as if we know what is supports. Each of these calls makes the runtime perform a check to see if the method call is actually supported. That is part of why dynamic typing has a runtime overhead.

Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
2

As everyone else has mentioned, the 'var' keyword does not give us dynamic typing. The type is inferred at compile time.

I use var when I think it makes sense; namely, when the type is obvious and it cuts down on the verbosity of a declaration:

var someDict = new Dictionary<string, int>();

Instead of...

Dictionary<string, int>() someDict = new Dictionary<string, int>();

However, in your example here:

var aName=getAllSomethings();

I wouldn't use 'var' because, as you have noticed already, you then have to check the return type of 'getAllSomethings()' (bad naming style for C# BTW) in order to know what 'aName' is, which just makes the code less readable.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 2
    Yes, but - and I've argued this before - it's the name of the function that's at wrong here, not `var`. If `var` ever makes something less readable, it's not the fault of `var`, it's the names you choose for your methods and variables that are wrong. – JulianR Sep 05 '10 at 21:34
  • 1
    I disagree. Do you actually put the return type of each method in the name of the method itself, i.e., "GetMemberIdListInt"? That is the nonsense they teach in intro to XXX language 101, good method naming does not require that sort of thing. – Ed S. Sep 05 '10 at 22:37
  • Though, in this case you are right; the method (and variable) name is terrible in the OP's example. – Ed S. Sep 05 '10 at 22:37
  • 2
    No, I wouldn't name it "GetMemberIdListInt", but I might call it "GetMemberIds". Sure, you don't immediately know that it is a List, but most operations I will do on it don't care about that. I can assume from the name that there is some sort of ID, and it is enumerable. – Chris Pitman Sep 06 '10 at 00:25
  • 1
    @Ed Swangren - As Chris said, I usually really don't care what the return type of a method is. My full opinion on `var` can be found here: http://stackoverflow.com/questions/3422719/how-does-implicit-typing-make-code-clearer/3428671#3428671 – JulianR Sep 06 '10 at 00:33