Boxing and Unboxing

var i = new int[2];

Is the "i" read in the box?

+5
source share
4 answers

There is nothing boxing. The var keyword does not mean the variable is boxxed. It does not perform any conversion at runtime. The var keyword is strictly a C # construct. What actually happens when using var:

var i = new int[1];

IL sees this as:

int[] i = new int[1]

Now, if you ask, if, when assigning int to part of the array, do I have this field?

eg:

i[0] = 2;

No, it is not.

This is the opposite of what it does:

var o = new object[1];

o[0] = 2;

In this example, and why using ArrayList (think extensible array) in 1.0, 1.1 (pre generics) was a huge cost. The following comment also applies to the example object[]:

, ArrayList, upcast Object. , , , . ; , .

MSDN ArrayList

#

+9

, # (var #), no, i . ( ).

, .

+7

# , . :

var i = 123;
object o = i;
+4

, (.. , ) . int. var keyword i, , .

:

var i = new object[2];

object[] i = new object[2], int, , . , , .

, var .

.NET. . NET Type.

+3
source

All Articles