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. PDF去除签名

    1.创建一个只有一页的PDF,用Acrobat打开.2.使用“文档->插入页面”,把有数字签名的文档插入到那一页后面.3.使用“文档->删除页面”,删除第一页,然后保存文档.

  2. groovy && java 混编 gradle 配置

    参考配置: apply plugin: "application" apply plugin: "java" apply plugin: "groov ...

  3. c++重在运算符

    运算符函数的定义与其他函数的定义类似,惟一的区别是运算符函数的函数名是由关键字operator和其后要重载的运算符符号构成的.运算符函数定义的一般格式如下: <返回类型说明符> opera ...

  4. numpy中文件的存储和读取-嵩天老师笔记

    numpy中csv文件的存储和读取 CSV文件:(Comma‐Separated Value, 逗号分隔值) 一维和二维数组 存储 np.savetxt(frame,array,fmt='%.18e' ...

  5. 2.JMeter查看结果树返回编码格式Unicode转为中文方法

    在使用JMeter做接口测试时,发现相同url,用postman工具,其返回数据参数为中文,而用JMeter工具,其返回参数为Unicode,如下图所示 解决方法如下: 1.Jmeter在对应的请求上 ...

  6. saiku3.8二次开发项目搭建(非maven)

    参考文章:http://blog.csdn.net/gsying1474/article/details/51603535 本文大部分参考了上面的博文,这里只是做一个记录,由于本人maven能力有限, ...

  7. Spring boot Freemarker 获取ContextPath的方法

    Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...

  8. 【学步者日记】C#反射中NonPublic和Instance需要一起使用

    完整链接请看: http://note.youdao.com/noteshare?id=f378d9a414e46893b0e300b017ed3655 ——————————————————————— ...

  9. Cocos2d-html5帧动画

    单独获取plist里面一个文件: cc.SpriteFrameCache.getInstance().addSpriteFrames(s_test_plist); var spriteTest2 = ...

  10. Python Tuples

    1. basic Tuples is a sequence of immutable object. It's a sequence, just like List. However, it cann ...