Array 中会发生装箱和拆箱吗?
Will Boxing and Unboxing happen in Array?
我是编程新手,
根据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.
我知道我们可以在数组列表中存储任何对象,因为 system.object
是所有类型的基础。装箱和拆箱发生在数组列表中。我同意。
装箱和拆箱会发生在数组中吗?因为我们可以像下面这样创建对象数组
object[] arr = new object[4] { 1, "abc", 'c', 12.25 };
我对在这样的数组中进行装箱和拆箱的理解是否正确?
Will boxing and unboxing happens in an array?
数组本身已经是引用类型,数组本身没有装箱。但是,由于您的一些元素是值类型(int
、double
和 char
),并且您的数组类型是 object
,因此将对所述元素进行装箱。当您想要提取它时,您需要将其拆箱:
var num = (int)arr[0];
在生成的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
是的,值类型元素(1、'c' 和 12.25)在放入 object[]
数组时将被装箱。
字符串 "abc" 将按原样放置,因为它是引用类型对象。
每次将值类型的值赋给object
类型的变量时,都会发生装箱操作,所以当您这样做时:
object[] arr = new object[4] { 1, "abc", 'c', 12.25 };
相当于
object[] arr = new object[4];
arr[0] = 1;
arr[1] = "abc";
arr[2] = 'c';
arr[3] = 12.25
将创建三个盒子来存储1、12.25和'c' 因为它们是值类型的值。
我是编程新手,
根据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.
我知道我们可以在数组列表中存储任何对象,因为 system.object
是所有类型的基础。装箱和拆箱发生在数组列表中。我同意。
装箱和拆箱会发生在数组中吗?因为我们可以像下面这样创建对象数组
object[] arr = new object[4] { 1, "abc", 'c', 12.25 };
我对在这样的数组中进行装箱和拆箱的理解是否正确?
Will boxing and unboxing happens in an array?
数组本身已经是引用类型,数组本身没有装箱。但是,由于您的一些元素是值类型(int
、double
和 char
),并且您的数组类型是 object
,因此将对所述元素进行装箱。当您想要提取它时,您需要将其拆箱:
var num = (int)arr[0];
在生成的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
是的,值类型元素(1、'c' 和 12.25)在放入 object[]
数组时将被装箱。
字符串 "abc" 将按原样放置,因为它是引用类型对象。
每次将值类型的值赋给object
类型的变量时,都会发生装箱操作,所以当您这样做时:
object[] arr = new object[4] { 1, "abc", 'c', 12.25 };
相当于
object[] arr = new object[4];
arr[0] = 1;
arr[1] = "abc";
arr[2] = 'c';
arr[3] = 12.25
将创建三个盒子来存储1、12.25和'c' 因为它们是值类型的值。