Does var keyword in C# cause boxing?

后端 未结 6 1003
余生分开走
余生分开走 2020-12-10 10:05

My boss forbids me to use var as it would cause boxing and slowing down the app.

Is that true?

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 10:43

    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...

提交回复
热议问题