简体   繁体   English

无论是装箱还是拆箱?

[英]Whether It is Boxing Or Unboxing?

int i = 5; 整数i = 5;

string str = i.ToString(); 字符串str = i.ToString();

String str1=(String) i.ToString(); 字符串str1 =(String)i.ToString();

As Int 's are value Type and String 's are reference Type 由于Int是值类型,而String是引用类型

so Whether It is Boxing Or Unboxing ??? 所以无论是装箱还是拆箱???

EDIT: Now For Second Statement Whether It is Boxing Or Unboxing ??? 编辑:现在对于第二条语句是装箱还是拆箱?

Your code is not an example of unboxing or boxing, but instead a method invocation of Int32.ToString() and assigning the return value to a string . 您的代码不是拆箱或装箱的示例,而是Int32.ToString()的方法调用,并将返回值分配给string The i.ToString() call doesn't assign an int to an object, but passes it to a method which returns a string . i.ToString()调用不会将int分配给对象,而是将其传递给返回string的方法。 The second line with the (string) cast is superfluous and the C# compiler doesn't even emit it into IL. 使用(string)强制转换的第二行是多余的,C#编译器甚至不会将其发送到IL中。

For example, if you had this in a main method: 例如,如果您在main方法中有此方法:

.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
// Method begins at RVA 0x2050
// Code size 19 (0x13)
.maxstack 1
.entrypoint
.locals init (
    [0] int32 i
)

IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldloca.s i
IL_0004: call instance string [mscorlib]System.Int32::ToString()
IL_0009: pop
IL_000a: ldloca.s i
IL_000c: call instance string [mscorlib]System.Int32::ToString() // cast isn't here
IL_0011: pop
IL_0012: ret
} // end of method Program::Main

If you were boxing an integer: 如果要装箱整数:

int i = 1; 
object iBox = i; 

emits: 发出:

.locals init (
    [0] int32 i,
    [1] object o
)

IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: box [mscorlib]System.Int32
IL_0009: stloc.1
IL_000a: ret

Notice the box op code. 注意box操作码。 If you aren't sure if something is boxing or unboxing, you can view the IL and see if this op code is there. 如果不确定是装箱还是拆箱,可以查看IL并查看该操作码是否在其中。

If you were unboxing an integer: 如果您要拆箱整数:

int j = (int) iBox;

The process is similar for other value types, like bool or double . 其他值类型(如booldouble的处理过程相似。

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. 装箱是将值类型转换为类型对象或该值类型实现的任何接口类型的过程。 Unboxing extracts the value type from the object. 取消装箱从对象中提取值类型。 In this case it is neither boxing nor unboxing. 在这种情况下,它既不是装箱也不是拆箱。

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. 当CLR装箱值类型时,它将值包装在System.Object中,并将其存储在托管堆中。

Unboxing extracts the value type from the object. 取消装箱从对象中提取值类型。 Boxing is implicit; 装箱是隐式的。 unboxing is explicit. 拆箱是明确的。

The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object. 装箱和拆箱的概念是类型系统的C#统一视图的基础,在该视图中,任何类型的值都可以视为对象。

In the following example, the integer variable i is boxed and assigned to object o. 在下面的示例中,将整数变量i装箱并分配给对象o。

C# C#

int i = 123;
// The following line boxes i. 
object o = i;  

The object o can then be unboxed and assigned to integer variable i: 然后可以将对象o拆箱并分配给整数变量i:

C# C#

o = 123;
i = (int)o;  // unboxing

boxing is happened when you assign your data to object data type. 将数据分配给对象数据类型时,将发生装箱操作。 since you are not doing this, there it is not boxing or unboxing. 由于您没有这样做,所以没有装箱或拆箱。

装箱用于将值类型(字符串除外)转换为对象类型,使用ToString()装箱方法装箱用于将对象类型转换为引用类型(字符串),使用anydatatype(字符串除外).parse()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM