A
Andre Kaufmann
Demetri said:So then what is wrong with boxing using the object type. I'm assuming this
"implicit" typing is there to allow one to assign a type that is not known
during design time?
Isn't var x = 1; essentially the same as object x = 1; ?
No. var x is an int value, object x is a boxed value.
E.g.:
var x = 1; x++; // is valid code
object x = 1; x++; // is invalid code
var x = 1.0 for example compiles to double x = 1.0
In BOTH cases if I want to add another number to x then I must know before
hand that what i'm adding is of type int, correct?
No. With var the type is specified and cannot be changed, like that
would be the case for a variant.
You may argue, well you may not know that what you're adding is an int, but
I argue that you will need to know somewhat in order to know that you CAN add
the to mathematically.
Good argument, but the compiler knows - even if you aren't sure about it
what type the variable is. Why should you know ? (in this case).
So i'm not sold on this var (implicit) typing thing. I certainly hope its
not a "workaround" to appease LINQ. Constructing a language to communicate
with one set of technolgies in mind is a bad plan in my estimation.
Anonymous types are needed for e.g. anonymous structures:
E.g.:
var s = new { a = 10, b = 1.2 };
Creates implicitly a structure with a hidden name. You haven't defined
the structure so you cannot use a type name.
I find var very convenient. E.g. if I don't have to care about type
returned by a function I simply can write:
var result = function();
function2(result);
Instead of:
MyObjectNameButToLong result = function();
function2(result);
Though I must admit that the code is somewhat harder to interpret. So I
wouldn't exceed the usage of var and use it only where it makes sense,
must be used and where it doesn't makes the code much harder to read /
interpret.
Andre