Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxing & Unboxing [duplicate]

Tags:

c#

.net

boxing

var i = new int[2];

Is "i" considered to be boxed?

like image 491
Vivek Avatar asked Jun 05 '10 13:06

Vivek


People also ask

How did boxing start?

On 6 January 1681, the first recorded boxing match took place in Britain when Christopher Monck, 2nd Duke of Albemarle (and later Lieutenant Governor of Jamaica) engineered a bout between his butler and his butcher with the latter winning the prize. Early fighting had no written rules.

What are the three types of boxing?

There are four generally accepted boxing styles that are used to define fighters. These are the swarmer, out-boxer, slugger, and boxer-puncher. Many boxers do not always fit into these categories, and it's not uncommon for a fighter to change their style over a period of time.

What is a boxing salary?

While ZipRecruiter is seeing annual salaries as high as $95,500 and as low as $18,500, the majority of Professional Boxer salaries currently range between $25,000 (25th percentile) to $40,000 (75th percentile) with top earners (90th percentile) making $71,500 annually across the United States.


1 Answers

No there is nothing to box. The var keyword doesn't mean that the variable is boxxed. It doesn't do any conversion during the runtime. The var keyword is strictly a C# construct. What is actually happening when you use var is:

var i = new int[1];

IL sees it as:

int[] i = new int[1]

Now if you are asking if when you assign an int to part of the array of i does it box?

such as:

i[0] = 2;

No it does not.

This is opposed to which does:

var o = new object[1];

o[0] = 2;

This example does and why using ArrayList (think expandable array) in 1.0, 1.1 (pre generics) was a huge cost. The following comment applies to the object[] example as well:

Any reference or value type that is added to an ArrayList is implicitly upcast to Object. If the items are value types, they must be boxed when added to the list, and unboxed when they are retrieved. Both the casting and the boxing and unboxing operations degrade performance; the effect of boxing and unboxing can be quite significant in scenarios where you must iterate over large collections.

MSDN Link to ArrayList

Link to Boxing in C#

like image 82
kemiller2002 Avatar answered Oct 16 '22 14:10

kemiller2002