My boss forbide me to use var as it would cause boxing and slowing down the app.
Is that true?
My boss forbide me to use var as it would cause boxing and slowing down the app.
Is that true?
Why are so many people cursed with bosses who are dumb? Revolution, brothers!
Your boss needs to read the documentation. var
causes the compiler to figure out the variable type by looking at the static type of the initialization expression. It doesn't make the slightest difference at runtime whether you specify the type by hand or you use var
and let the compiler figure it out for you.
Update In a comment under the question, Hans Passant asks
can you think of any var initializer that causes boxing without using a cast?
An example of a self-contained expression that forces such a conversion is:
var boxedInt = new Func<int, object>(n => n)(5);
But that is just identical to:
object boxedInt = new Func<int, object>(n => n)(5);
In other words, this doesn't really have anything to do with var
. The result of my initializer expression is object
, hence var
has to use that as the type of the variable. It couldn't be anything else.
That's not true at all.
var
just means "dear compiler, I know what the type is, and so do you, so let's just move on shall we."
It makes the code shorter and some find this more readable (others find it less readable), but there's no performance penalty whatsoever.
An approach that might work is to write these two methods:
public static void WithInt()
{
int x = 5;
Console.WriteLine(x);
}
public static void WithVar()
{
var x = 5;
Console.WriteLine(x);
}
Compile, and use ildasm
to examine the produced CIL. Show your boss.
edit @ck has done all but the last step for you :)
Following on from Aakash's answer, here is the IL: (thanks LINQPad)
WithInt:
IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: call System.Console.WriteLine
IL_0008: ret
WithVar:
IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: call System.Console.WriteLine
IL_0008: ret
Conceptually, I wouldn't trust var
either. I don't really want my variables to also have a variable type; that's just begging for disaster.
Maybe your boss is an old Visual Basic (as in <= 6.0) programmer used to the VARIANT
type. If you didn't specify the type of your variable explicitly in your DIM
statement, it was a VARIANT
which is a sort of union
if I recall correctly. You could view this as a sort of "boxing" and "unboxing" when passing such variables to functions.
Sometimes people get confused. Ask your boss about his Visual Basic war stories. Listen, learn and earn some sympathy at the same time! As you leave the office you could point out that the c# compiler figures this stuff out at compile time and that "boxing" isn't an issue anymore.
Don't expect your boss to have to keep up with the newest changes to languages/APIs. This isn't about being dumb. It's about having other stuff to do. His job, for instance.
Edit: As noted in comments below, though, telling you not to use var
for the wrong reasons is probably not his job...