装箱/拆箱和类型铸造有什么不同?

时间:2022-01-25 16:35:53

What is the difference between boxing/unboxing and type casting?

装箱/拆箱和类型铸造有什么不同?

Often, the terms seem to be used interchangeably.

通常,这些术语似乎可以互换使用。

5 个解决方案

#1


41  

Boxing refers to a conversion of a non-nullable-value type into a reference type or the conversion of a value type to some interface that it implements (say int to IComparable<int>). Further, the conversion of an underlying value type to a nullable type is also a boxing conversion. (Caveat: Most discussions of this subject will ignore the latter two types of conversions.)

装箱指的是将非空值类型转换为引用类型,或者将值类型转换为它实现的某个接口(比如int to icom克莱伯 )。此外,将底层值类型转换为可空类型也是装箱转换。(注意:大多数关于这一主题的讨论将忽略后两种转换。)

For example,

例如,

int i = 5;
object o = i;

converts i to an instance of type object.

将i转换为类型对象的实例。

Unboxing refers to an explicit conversion from an instance of object or ValueType to a non-nullable-value type, the conversion of an interface type to a non-nullable-value type (e.g., IComparable<int> to int). Further, the conversion of a nullable type to the underlying type is also an unboxing conversion. (Caveat: Most discussion of this subject will ignore the latter two types of conversions.)

Unboxing指从对象或ValueType实例到非nullable-value类型的显式转换,将接口类型转换为非nullable-value类型(例如,icom克莱伯 到int)。此外,可空类型到底层类型的转换也是一种反装箱转换。(注意:大多数关于这一主题的讨论将忽略后两种转换。)

For example,

例如,

object o = (int)5;
int i = (int)o;

converts the integer boxed in o to an instance of type int.

将o框中的整数转换为int类型的实例。

A type cast is an explicit conversion of an expression to a given type. Thus

类型转换是表达式到给定类型的显式转换。因此

(type) expression

explicitly converts expression to an object of type type.

显式地将表达式转换为类型类型的对象。

#2


19  

Boxing and unboxing is a subset of type casts. Boxing is the act of treating a value type as reference type (which in practice, involves copying the contents of that value type (from stack) to the heap and returning a reference to that object). This allows a value type to be passed wherever a compatible reference type is expected. It also allows virtual method calls and other features of reference types to be performed on the value type. Unboxing is the reverse of this operation (getting back a value type out of a boxed object).

装箱和拆箱是类型强制转换的一个子集。装箱是将值类型视为引用类型的行为(在实践中,它涉及将该值类型的内容(从堆栈)复制到堆并返回对该对象的引用)。这允许在预期的兼容引用类型的地方传递值类型。它还允许对值类型执行虚方法调用和引用类型的其他特性。取消装箱是这个操作的反向操作(从盒装对象中获取值类型)。

Type cast is the term used for any type of conversion from a variable of specific type to another. It's a broader concept.

类型转换是用于从特定类型的变量到另一个类型的任何类型转换的术语。这是一个更广泛的概念。

A few minutes ago I answered a related question that covers this difference. To summarize, I categorized different types of IL instructions generated by C# cast operator:

几分钟前,我回答了一个与此相关的问题。综上所述,我将c# cast算符生成的IL指令的不同类型进行了分类:

  1. Boxing (box IL instruction) and unboxing (unbox IL instruction)
  2. 装箱(盒IL指令)和开箱(开箱IL指令)
  3. Casting through the inhertiance hierarchy (like dynamic_cast<Type> in C++, uses castclass IL instruction to verify)
  4. 通过继承层次结构(比如dynamic_cast< c++中的>类型,使用castclass IL指令进行验证)
  5. Casting between primitive types (like static_cast<Type> in C++, there are plenty of IL instructions for different types of casts between primitive types)
  6. 在基元类型之间进行转换(如c++中的static_cast ,在基元类型之间有许多不同类型的转换的IL指令)
  7. Calling user defined conversion operators (at the IL level they are just method calls to the appropriate op_XXX method).
  8. 调用用户定义的转换操作符(在IL级,它们只是对适当的op_XXX方法的方法调用)。

#3


15  

Boxing is the term for turning a value type (int, double, float, Guid, etc.) into a reference type (System.Object, System.String, etc.). Doing this boxing operation allocates memory on the heap (which the garbage collector will eventually need to reclaim). Unboxing is the reverse of this process, taking a reference type and turning it into a value type.

装箱是将值类型(int, double, float, Guid等)转换为引用类型(System)的术语。对象,系统。字符串,等等)。执行此装箱操作将在堆上分配内存(垃圾收集器最终需要回收内存)。取消装箱与此过程相反,接受引用类型并将其转换为值类型。

Casting is taking a type (say, System.Object) and treating it as another type (say, System.String).

类型转换是获取一个类型(比如System.Object)并将其视为另一个类型(比如System.String)。

When you box something in C#, you are casting it to another type. The difference is that it allocates additional memory as a new reference type is created.

当您在c#中框入某样东西时,您正在将它转换为另一种类型。不同之处在于,当创建新的引用类型时,它会分配额外的内存。

Bottom line: boxing is a special kind of cast that converts a value type to a reference type, which requires the allocation of a new reference type.

底线:装箱是一种特殊的类型转换,它将值类型转换为引用类型,这需要分配新的引用类型。

#4


4  

Boxing/unboxing and type casting are two different operations, however they use the same syntax.

装箱/拆箱和类型转换是两种不同的操作,但是它们使用相同的语法。

They are only used interchangeably when the person talking about it doesn't know what's really happening...

它们只有在谈论它的人不知道到底发生了什么时才可以互换使用。

Boxing is storing a value type as an object on the heap, and unboxing is reading the value from the object. You can only unbox the value as it's exact type.

装箱是将值类型存储为堆上的对象,而开箱是从对象中读取值。您只能打开该值,因为它是确切的类型。

Casting is when you convert a basic type to another basic type (like from an int to a long), or when you change the type of a reference (like from List<int> to IEnumerable<int>).

类型转换是将基本类型转换为另一个基本类型(如从int到long),或者更改引用的类型(如从List 到IEnumerable )。

#5


3  

Boxing means converting a value type variable (i.e. an integer) to a reference type. Unboxing is the reverse of that, using type casting. In the .NET world, everything derives from the "object" type in a nutshell.

装箱意味着将值类型变量(即整数)转换为引用类型。反装箱则相反,使用类型转换。在。net世界中,所有东西都简单地源自“对象”类型。

For example (C# example):

例如(c#示例):

int myInt = 0;                 // original variable (unboxed to begin with)
object boxed = myInt;          // box it up
int myIntUnBoxed = (int)boxed; // and unbox it again using type casting

The take-away from this is the unification of the type system, allowing value-types to be treated as reference types. This article has a more indepth look at boxing/unboxing.

这是类型系统的统一,允许将值类型作为引用类型处理。这篇文章更深入地介绍了拳击/反拳击。

#1


41  

Boxing refers to a conversion of a non-nullable-value type into a reference type or the conversion of a value type to some interface that it implements (say int to IComparable<int>). Further, the conversion of an underlying value type to a nullable type is also a boxing conversion. (Caveat: Most discussions of this subject will ignore the latter two types of conversions.)

装箱指的是将非空值类型转换为引用类型,或者将值类型转换为它实现的某个接口(比如int to icom克莱伯 )。此外,将底层值类型转换为可空类型也是装箱转换。(注意:大多数关于这一主题的讨论将忽略后两种转换。)

For example,

例如,

int i = 5;
object o = i;

converts i to an instance of type object.

将i转换为类型对象的实例。

Unboxing refers to an explicit conversion from an instance of object or ValueType to a non-nullable-value type, the conversion of an interface type to a non-nullable-value type (e.g., IComparable<int> to int). Further, the conversion of a nullable type to the underlying type is also an unboxing conversion. (Caveat: Most discussion of this subject will ignore the latter two types of conversions.)

Unboxing指从对象或ValueType实例到非nullable-value类型的显式转换,将接口类型转换为非nullable-value类型(例如,icom克莱伯 到int)。此外,可空类型到底层类型的转换也是一种反装箱转换。(注意:大多数关于这一主题的讨论将忽略后两种转换。)

For example,

例如,

object o = (int)5;
int i = (int)o;

converts the integer boxed in o to an instance of type int.

将o框中的整数转换为int类型的实例。

A type cast is an explicit conversion of an expression to a given type. Thus

类型转换是表达式到给定类型的显式转换。因此

(type) expression

explicitly converts expression to an object of type type.

显式地将表达式转换为类型类型的对象。

#2


19  

Boxing and unboxing is a subset of type casts. Boxing is the act of treating a value type as reference type (which in practice, involves copying the contents of that value type (from stack) to the heap and returning a reference to that object). This allows a value type to be passed wherever a compatible reference type is expected. It also allows virtual method calls and other features of reference types to be performed on the value type. Unboxing is the reverse of this operation (getting back a value type out of a boxed object).

装箱和拆箱是类型强制转换的一个子集。装箱是将值类型视为引用类型的行为(在实践中,它涉及将该值类型的内容(从堆栈)复制到堆并返回对该对象的引用)。这允许在预期的兼容引用类型的地方传递值类型。它还允许对值类型执行虚方法调用和引用类型的其他特性。取消装箱是这个操作的反向操作(从盒装对象中获取值类型)。

Type cast is the term used for any type of conversion from a variable of specific type to another. It's a broader concept.

类型转换是用于从特定类型的变量到另一个类型的任何类型转换的术语。这是一个更广泛的概念。

A few minutes ago I answered a related question that covers this difference. To summarize, I categorized different types of IL instructions generated by C# cast operator:

几分钟前,我回答了一个与此相关的问题。综上所述,我将c# cast算符生成的IL指令的不同类型进行了分类:

  1. Boxing (box IL instruction) and unboxing (unbox IL instruction)
  2. 装箱(盒IL指令)和开箱(开箱IL指令)
  3. Casting through the inhertiance hierarchy (like dynamic_cast<Type> in C++, uses castclass IL instruction to verify)
  4. 通过继承层次结构(比如dynamic_cast< c++中的>类型,使用castclass IL指令进行验证)
  5. Casting between primitive types (like static_cast<Type> in C++, there are plenty of IL instructions for different types of casts between primitive types)
  6. 在基元类型之间进行转换(如c++中的static_cast ,在基元类型之间有许多不同类型的转换的IL指令)
  7. Calling user defined conversion operators (at the IL level they are just method calls to the appropriate op_XXX method).
  8. 调用用户定义的转换操作符(在IL级,它们只是对适当的op_XXX方法的方法调用)。

#3


15  

Boxing is the term for turning a value type (int, double, float, Guid, etc.) into a reference type (System.Object, System.String, etc.). Doing this boxing operation allocates memory on the heap (which the garbage collector will eventually need to reclaim). Unboxing is the reverse of this process, taking a reference type and turning it into a value type.

装箱是将值类型(int, double, float, Guid等)转换为引用类型(System)的术语。对象,系统。字符串,等等)。执行此装箱操作将在堆上分配内存(垃圾收集器最终需要回收内存)。取消装箱与此过程相反,接受引用类型并将其转换为值类型。

Casting is taking a type (say, System.Object) and treating it as another type (say, System.String).

类型转换是获取一个类型(比如System.Object)并将其视为另一个类型(比如System.String)。

When you box something in C#, you are casting it to another type. The difference is that it allocates additional memory as a new reference type is created.

当您在c#中框入某样东西时,您正在将它转换为另一种类型。不同之处在于,当创建新的引用类型时,它会分配额外的内存。

Bottom line: boxing is a special kind of cast that converts a value type to a reference type, which requires the allocation of a new reference type.

底线:装箱是一种特殊的类型转换,它将值类型转换为引用类型,这需要分配新的引用类型。

#4


4  

Boxing/unboxing and type casting are two different operations, however they use the same syntax.

装箱/拆箱和类型转换是两种不同的操作,但是它们使用相同的语法。

They are only used interchangeably when the person talking about it doesn't know what's really happening...

它们只有在谈论它的人不知道到底发生了什么时才可以互换使用。

Boxing is storing a value type as an object on the heap, and unboxing is reading the value from the object. You can only unbox the value as it's exact type.

装箱是将值类型存储为堆上的对象,而开箱是从对象中读取值。您只能打开该值,因为它是确切的类型。

Casting is when you convert a basic type to another basic type (like from an int to a long), or when you change the type of a reference (like from List<int> to IEnumerable<int>).

类型转换是将基本类型转换为另一个基本类型(如从int到long),或者更改引用的类型(如从List 到IEnumerable )。

#5


3  

Boxing means converting a value type variable (i.e. an integer) to a reference type. Unboxing is the reverse of that, using type casting. In the .NET world, everything derives from the "object" type in a nutshell.

装箱意味着将值类型变量(即整数)转换为引用类型。反装箱则相反,使用类型转换。在。net世界中,所有东西都简单地源自“对象”类型。

For example (C# example):

例如(c#示例):

int myInt = 0;                 // original variable (unboxed to begin with)
object boxed = myInt;          // box it up
int myIntUnBoxed = (int)boxed; // and unbox it again using type casting

The take-away from this is the unification of the type system, allowing value-types to be treated as reference types. This article has a more indepth look at boxing/unboxing.

这是类型系统的统一,允许将值类型作为引用类型处理。这篇文章更深入地介绍了拳击/反拳击。