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;
- C#的基本类型的封箱(boxing)与拆箱(unboxing)
- C#的Boxing/Unboxing解析
- C#装箱(Boxing)和拆箱(Unboxing)
- C#装箱和拆箱(Boxing 和 UnBoxing)
- C#—装箱(boxing)与拆箱(unboxing)
- 装箱(boxing)与拆箱(unboxing)
- C#中的“装箱”(boxing)与“拆箱”(unboxing)
- 基本类型的封箱与装箱
- 减少装箱(Boxing)和拆箱(Unboxing)操作(感觉有用,抄过来的)
- C#中装箱和拆箱的作用 the effection of boxing and unboxing
- 装箱(Boxing)和拆箱(Unboxing)
- 装箱(Boxing)和拆箱(Unboxing)
- [转]装箱(Boxing)和拆箱(Unboxing)
- 装箱(boxing)和拆箱(unboxing)
- 自动装箱(boxing)和自动拆箱(unboxing)
- 装箱(boxing)和拆箱(unboxing) [转]
- java 自动拆箱(unboxing)和自动装箱(boxing)
- C#学习日记18----装箱(boxing)转换与拆箱(unboxing)转换
- autotools用法简介
- 2012年7月第4周
- DAO+servlet+JSP是如何体现MVC设计模式的
- html5
- sicily--1021. Couples
- C#的基本类型的封箱(boxing)与拆箱(unboxing)
- poj 2197 hoj 2027 Jill's Tour Paths 搜索
- 什么是静态网站?什么是动态网站?
- 什么是静态网站?什么是动态网站?
- linux命令详细全解
- codeforces 208C 图的拓扑排序+简单DP
- hdu LCIS(线段树)
- SWUN 1076 - 中州之旅
- redhat 6 配置 yum 源的两种方法