My boss forbids me to use var
as it would cause boxing and slowing down the app.
Is that true?
C# 3 added the keyword "var". This allows for local type inference when the compiler can unequivocally determine what type the variable should be.
By using “var”, you are giving full control of how a variable will be defined to someone else. You are depending on the C# compiler to determine the datatype of your local variable – not you. You are depending on the code inside the compiler – someone else's code outside of your code to determine your data type.
The var keyword can be used in place of a type when declaring a variable to allow the compiler to infer the type of the variable.
This means that var is an array of pointers.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With