boxing & unboxing

  Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.  

int i = ;
// The following line boxes i.
object o = i; o = ;
i = (int)o; // unboxing

  In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see Performance.

  Unboxing只能对应同一类型,否则出错,见下例:

class TestUnboxing
{
static void Main()
{
int i = ;
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);
}
}
}

参考:http://msdn.microsoft.com/zh-cn/library/yz2be5wk.aspx

boxing & unboxing的更多相关文章

  1. [Done]FindBugs: boxing/unboxing to parse a primitive

    在开发过程中遇到了以下问题: FindBugs: boxing/unboxing to parse a primitive 查看代码(左边是老代码,右边是新的): 问题出在 自动装箱和拆箱的检查. 参 ...

  2. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  3. @SuppressWarnings注解的用法

    一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的"感叹号"就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @S ...

  4. CLR via C# 3rd - 05 - Primitive, Reference, and Value Types

    1. Primitive Types        Any data types the compiler directly supports are called primitive types. ...

  5. Dictionary和Hashtable的一些异同

    Dictionary和Hashtable 区别: Dictionary和Hashtable 区别 Dictionary Hashtable  支持范型 不支持 需要自己做线程同步 通过调用 Synch ...

  6. [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换

    [.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...

  7. Java程序员的日常—— 基于类的策略模式、List<?>与List、泛型编译警告、同比和环比

    早晨起得太早,昨晚睡得太晚,一天都迷迷糊糊的.中午虽然睡了半个小时,可是依然没有缓过来.整个下午都在混沌中....不过今天下载了一款手游--<剑侠情缘>,感觉不错,喜欢这种类型的游戏. 今 ...

  8. @SuppressWarnings忽略警告

    简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...

  9. @SuppressWarnings的使用、作用、用法

    在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决 例如:@S ...

随机推荐

  1. CentOS配置网易163 yum源

    使用说明 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...

  2. 1.JMeter===添加响应断言

    断言即Lr中的检查点,我们在进行测试时,需要对每次请求测试的返回做检验 1.以百度做案例,添加线程组==添加HTTP请求==添加查看结果树 2.在HTTP请求下添加响应断言 注:模式匹配规则,比较常用 ...

  3. input type=number 禁止输入字符“e”的办法

    输入框input,的type设置为number,本想只输入数字,但是字符“e”却能通过, 首先科普一下, <body> <input onkeypress="getCode ...

  4. 使用GDI+保存带Alpha通道的图像

    带Alpha通道的图像(ARBG)在通过GDIPlus::Bitmap::FromHBITMAP等转为GDI+位图,再存储时,透明区域会变成纯黑(也有可能是纯白?).   网上找了两段保持透明的实现代 ...

  5. LogUtils日志管理工具

    public class LogUtils { public static final int VERBOSE = 1; public static final int DEBUG = 2; publ ...

  6. python 生成器和生成器表达式

    1.生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样.取值方式和迭代器一样(__next__(),send():给上一个yield传值) 生成器一般由生成器函数或者生成器表达式来创建 其实就是 ...

  7. 关于adb devices连不上手机的几种情况

    启动adb 1.打开cmd首次使用adb devices,出现下面这两句,就是正在启动adb服务 C:\Users\admin>adb devices List of devices attac ...

  8. [Java.Web][Servlet]常用请求头

    response.setStatus(302); response.setHeader("location", "/day04/1.html"); 这段代码可以 ...

  9. 将view添加到地图覆盖物

    原文地址:http://my.oschina.net/freestyletime/blog/291638 官方例子 这个百度地图 android SDK 关于基础地图覆盖物的例子 http://dev ...

  10. cloudera manager卸载流程

    注意:卸载Cloudera Manager后,根据需要保留或者删除集群中的Hadoop数据.下面的命令没有删除Hadoop数据,可以在控制台的Hadoop 和MapReduce /配置/选项卡,查看H ...