Boxing and Unboxing
来源:互联网 发布:移动端适配seo 编辑:程序博客网 时间:2024/05/01 15:46
Boxing and Unboxing
Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object o.
int i = 123;object o = (object)i; // boxing
The object o can then be unboxed and assigned to integer variable i:
o = 123;i = (int)o; // unboxing
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value
type to the type object or to any interface type implemented by this value type. Boxing a value type allocates
an object instance on the heap and copies the value into the new object.
Consider the following declaration of a value-type variable:
int i = 123;
The following statement implicitly applies the boxing operation on the variable i:
object o = i; // implicit boxing
This example converts an integer variable i to an object o by using boxing. Then, the value stored in the variable
i is changed from 123 to 456. The example shows that the original value type and the boxed object use separate
memory locations, and therefore can store different values.
class TestBoxing{ static void Main() { int i = 123; object o = i; // implicit boxing i = 456; // change the contents of i System.Console.WriteLine("The value-type value = {0}", i); System.Console.WriteLine("The object-type value = {0}", o); }}
Output:
The value-type value = 456
The object-type value = 123
Unboxing is an explicit conversion(只能explicit转换) from the type object to a value type or from an interface type to a value type that
implements the interface. An unboxing operation consists of:
Checking the object instance to make sure that it is a boxed value of the given value type.
Copying the value from the instance into the value-type variable.
Example:
class TestUnboxing
{
static void Main()
{
int i = 123;
object o = i; // implicit boxing
try
{
int j = (short)o; // attempt to unbox
System.Console.WriteLine("Unboxing OK.");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
}
}
}
Output:
Specified cast is not valid. Error: Incorrect unboxing.
If you change the statement:
int j = (short)o;
to:
int j = (int)o;
the conversion will be performed, and you will get the output:
Unboxing OK.
- 11.3 Boxing and unboxing
- Boxing and Unboxing
- Boxing and Unboxing
- Boxing and Unboxing
- BOXING AND UNBOXING FOR BEGINNER
- Boxing and unboxing in C#
- boxing, unboxing
- Java Auto Boxing and Unboxing -Java 学习笔记 (23)
- CLR 是怎样去处理Boxing and Unboxing Value Types?
- Effective C#之17:Minimize Boxing and Unboxing.
- 关于Boxing和Unboxing
- 关于boxing 和 unboxing
- C#的Boxing/Unboxing解析
- java中的Boxing和unboxing
- java中的auto-boxing/unboxing
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- C#中装箱和拆箱的作用 the effection of boxing and unboxing
- C#中的"装箱"(boxing)与"拆箱"(unboxing)
- 用poi3和chartdirect生成带图表的excel报表
- .net的一些术语
- java classLoader 原则
- A Contributor's Guide to Launchpad.net - Part 2 - Code Management
- 子类引用指向父类对象???
- Boxing and Unboxing
- 网页上点击一个按钮,打开服务器上的一个应用程序
- Import module问题的解决V2与python CGI访问的实现
- 关于asp.net web service的一点疑问----"为什么vs2005还要多生成四个文件"
- 分布式数据库访问解决方案
- windows media player 控件的常用属性及方法 属性/方法名
- 项目开发心得之需求分析
- 安装软件出现Error 1327 Invalid Drive X:/(X为盘符,有可能是C~Z)的解决方法
- Head First C# 中文版 第10章 异常处理 page466