Will Boxing and Unboxing happen in Array?
Asked Answered
C

3

14

I'm new to programming,

As per MSDN,

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.

I knew We can store any objects in an arraylist, because system.object is a base for all all types. Boxing and unboxing happens in array list. I agree with that.

Will boxing and unboxing happens in an array? Because We can create object array like below

object[] arr = new object[4] { 1, "abc", 'c', 12.25 };

Is my understanding that boxing and unboxing happens in such array correct?

Correll answered 22/5, 2016 at 6:22 Comment(5)
There is no boxing here, since your array is already of type object. there is nothing to box in that case.Rolfe
@ Zohar - But object type is the default type for array list right, But boxing and unboxing happens thereCorrell
How come you are saying boxing is not happening here, Because here the type is object, boxing should happens right?Correll
@ZoharPeled Are you sure about that? How are value types stored in objects then? In this particular case you'd hjave 3 boxing operations for the values 1, 'c' and 12.25Daudet
Read Yuval's answer, that's what I mean - the array itself doesn't cause the boxing, the assignment of ints, strings, chars and floats into objects is of course boxing, but an explicit one.Rolfe
F
12

Will boxing and unboxing happens in an array?

The Array itself is already a reference type, there is no boxing on the array itself. But, as some of your elements are value types (int, double and char), and your array type is object, a boxing will occur for the said element. When you'll want to extract it, you'll need to unbox it:

var num = (int)arr[0];

You can see it in the generated IL:

IL_0000: ldarg.0
IL_0001: ldc.i4.4
IL_0002: newarr [mscorlib]System.Object
IL_0007: dup
IL_0008: ldc.i4.0
IL_0009: ldc.i4.1
IL_000a: box [mscorlib]System.Int32 // Boxing of int
IL_000f: stelem.ref
IL_0010: dup
IL_0011: ldc.i4.1
IL_0012: ldstr "abc"
IL_0017: stelem.ref
IL_0018: dup
IL_0019: ldc.i4.2
IL_001a: ldc.i4.s 99
IL_001c: box [mscorlib]System.Char
IL_0021: stelem.ref
IL_0022: dup
IL_0023: ldc.i4.3
IL_0024: ldc.r8 12.25
IL_002d: box [mscorlib]System.Double
IL_0032: stelem.ref
IL_0033: stfld object[] C::arr
IL_0038: ldarg.0
IL_0039: call instance void [mscorlib]System.Object::.ctor()
IL_003e: nop
IL_003f: ret
Flack answered 22/5, 2016 at 6:35 Comment(0)
G
6

Yes, value type elements (1, 'c' and 12.25) will be boxed when placed into array of object[].

String "abc" will be placed as is since it is reference type object.

Gabbey answered 22/5, 2016 at 6:36 Comment(0)
D
5

Every time you assign a value of a value type to a variable of type object a boxing operation will occur, So when you do:

object[] arr = new object[4] { 1, "abc", 'c', 12.25 };

Which is equivalent to

object[] arr = new object[4];
arr[0] = 1;
arr[1] = "abc";
arr[2] = 'c';
arr[3] = 12.25

Three boxes will be created to store 1, 12.25 and 'c' because they are values of value types.

Daudet answered 22/5, 2016 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.