Why use dynamic typing in c#?
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 confusio开发者_运维技巧n 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.
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.
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.
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.
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.
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.
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.
精彩评论