Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does var keyword in C# cause boxing?

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

Is that true?

like image 367
msfanboy Avatar asked Aug 24 '10 10:08

msfanboy


People also ask

Does C# have var?

C# 3 added the keyword "var". This allows for local type inference when the compiler can unequivocally determine what type the variable should be.

Why do we use var in C#?

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.

What coding language has VAR?

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.

What does VAR mean in C++?

This means that var is an array of pointers.


1 Answers

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 :)

like image 154
AakashM Avatar answered Oct 04 '22 09:10

AakashM