C#的基本类型的封箱(boxing)与拆箱(unboxing)

来源:互联网 发布:日本东南亚经济知乎 编辑:程序博客网 时间:2024/04/27 16:36

boxing and unboxing

In Chapter 2, “Core C#,” you learned that all types, both the simple predefined types such asint andchar,and the complex types such as classes and structs, derive from theobject type. This means that you cantreat even literal values as though they are objects:

string s = 10.ToString();

However, you also saw that C# data types are divided into value types, which are allocated on the stack,and reference types, which are allocated on the heap. How does this square with the ability to call methodson an int,if the intis nothing more than a 4-byte value on the stack?

The way C# achieves this is through a bit of magic calledboxing. Boxing and its counterpart,unboxing,allow you to convert value types to reference types and then back to value types. We include this in thesection on casting because this is essentially what you are doing — you are casting your value to theobjecttype. Boxing is the term used to describe the transformation of a value type to a reference type. Basically,the runtime creates a temporary reference-type box for the object on the heap.

This conversion can occur implicitly, as in the preceding example, but you can also perform it explicitly:

int myIntNumber = 20;
object myObject = myIntNumber;

Unboxing is the term used to describe the reverse process, where the value of a previously boxed value typeis cast back to a value type. We use the termcast here, because this has to be done explicitly. The syntax issimilar to explicit type conversions already described:

int myIntNumber = 20;
object myObject = myIntNumber; // Box the int
int mySecondNumber = (int)myObject; // Unbox it back into an int

You can only unbox a variable that has previously been boxed. If you execute the last line whenmyObject isnot a boxed int,you will get an exception thrown at runtime.

One word of warning: when unboxing, you have to be careful that the receiving value variable has enoughroom to store all the bytes in the value being unboxed. C#’sints, for example, are only 32 bits long, sounboxing along value (64 bits) into anint as shown here will result in anInvalidCastException:

long myLongNumber = 333333423;
object myObject = (object)myLongNumber;int myIntNumber = (int)myObject; 

原创粉丝点击