为什么在c#中需要装箱和拆箱?
我知道什么是装箱和开箱,但我不理解它的真正用途。为什么,在哪里使用?
short s = 25;
object objshort = s; //Boxing
short anothershort = (short)objshort; //Unboxing
为什么在c#中需要装箱和拆箱?
我知道什么是装箱和开箱,但我不理解它的真正用途。为什么,在哪里使用?
short s = 25;
object objshort = s; //Boxing
short anothershort = (short)objshort; //Unboxing
当前回答
当我们有一个需要object作为参数的函数,但我们有不同的值类型需要传递时,装箱是必需的,在这种情况下,我们需要在将值类型传递给函数之前首先将值类型转换为对象数据类型。
我不认为这是真的,试试这个:
class Program
{
static void Main(string[] args)
{
int x = 4;
test(x);
}
static void test(object o)
{
Console.WriteLine(o.ToString());
}
}
这运行得很好,我没有使用装箱/拆箱。(除非编译器在幕后做这些?)
其他回答
一般来说,您通常会希望避免对值类型进行装箱。
然而,在极少数情况下,这是有用的。例如,如果您需要以1.1框架为目标,那么您将无法访问泛型集合。在. net 1.1中使用任何集合都需要将您的值类型视为系统。对象,该对象导致装箱/解装箱。
There are still cases for this to be useful in .NET 2.0+. Any time you want to take advantage of the fact that all types, including value types, can be treated as an object directly, you may need to use boxing/unboxing. This can be handy at times, since it allows you to save any type in a collection (by using object instead of T in a generic collection), but in general, it is better to avoid this, as you're losing type safety. The one case where boxing frequently occurs, though, is when you're using Reflection - many of the calls in reflection will require boxing/unboxing when working with value types, since the type is not known in advance.
装箱并不是你真正使用的东西——它是运行时使用的东西,这样你就可以在必要时以同样的方式处理引用和值类型。例如,如果您使用ArrayList来保存整数列表,则整数将被装箱以适应ArrayList中的对象类型插槽。
现在使用泛型集合,这种情况几乎消失了。如果你创建一个List<int>,没有装箱完成- List<int>可以直接保存整数。
最后一个我不得不开箱的地方是从数据库中检索数据的代码(我没有使用LINQ to SQL,只是简单的旧ADO.NET):
int myIntValue = (int)reader["MyIntValue"];
基本上,如果使用泛型之前的旧api,就会遇到装箱。除此之外,这种情况并不常见。
当一个方法只接受一个引用类型作为参数时(比如一个泛型方法通过new约束被约束为一个类),您将不能将引用类型传递给它,而必须对它进行装箱。
对于任何以object作为参数的方法也是如此——这必须是引用类型。
装箱是将值转换为引用类型,其中数据位于堆上对象的某个偏移量处。
至于拳击到底是做什么的。下面是一些例子
单C + +
void* mono_object_unbox (MonoObject *obj)
{
MONO_EXTERNAL_ONLY_GC_UNSAFE (void*, mono_object_unbox_internal (obj));
}
#define MONO_EXTERNAL_ONLY_GC_UNSAFE(t, expr) \
t result; \
MONO_ENTER_GC_UNSAFE; \
result = expr; \
MONO_EXIT_GC_UNSAFE; \
return result;
static inline gpointer
mono_object_unbox_internal (MonoObject *obj)
{
/* add assert for valuetypes? */
g_assert (m_class_is_valuetype (mono_object_class (obj)));
return mono_object_get_data (obj);
}
static inline gpointer
mono_object_get_data (MonoObject *o)
{
return (guint8*)o + MONO_ABI_SIZEOF (MonoObject);
}
#define MONO_ABI_SIZEOF(type) (MONO_STRUCT_SIZE (type))
#define MONO_STRUCT_SIZE(struct) MONO_SIZEOF_ ## struct
#define MONO_SIZEOF_MonoObject (2 * MONO_SIZEOF_gpointer)
typedef struct {
MonoVTable *vtable;
MonoThreadsSync *synchronisation;
} MonoObject;
在Mono中打开一个对象是一个在对象中2个gpointer偏移量处强制转换指针的过程(例如16字节)。gpointer是一个void*。在查看MonoObject的定义时,这是有意义的,因为它显然只是数据的头部。
C++
在c++中,你可以这样做:
#include <iostream>
#define Object void*
template<class T> Object box(T j){
return new T(j);
}
template<class T> T unbox(Object j){
T temp = *(T*)j;
delete j;
return temp;
}
int main() {
int j=2;
Object o = box(j);
int k = unbox<int>(o);
std::cout << k;
}