boxing & unboxing
【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的更多相关文章
- [Done]FindBugs: boxing/unboxing to parse a primitive
在开发过程中遇到了以下问题: FindBugs: boxing/unboxing to parse a primitive 查看代码(左边是老代码,右边是新的): 问题出在 自动装箱和拆箱的检查. 参 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- @SuppressWarnings注解的用法
一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的"感叹号"就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @S ...
- CLR via C# 3rd - 05 - Primitive, Reference, and Value Types
1. Primitive Types Any data types the compiler directly supports are called primitive types. ...
- Dictionary和Hashtable的一些异同
Dictionary和Hashtable 区别: Dictionary和Hashtable 区别 Dictionary Hashtable 支持范型 不支持 需要自己做线程同步 通过调用 Synch ...
- [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换
[.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...
- Java程序员的日常—— 基于类的策略模式、List<?>与List、泛型编译警告、同比和环比
早晨起得太早,昨晚睡得太晚,一天都迷迷糊糊的.中午虽然睡了半个小时,可是依然没有缓过来.整个下午都在混沌中....不过今天下载了一款手游--<剑侠情缘>,感觉不错,喜欢这种类型的游戏. 今 ...
- @SuppressWarnings忽略警告
简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...
- @SuppressWarnings的使用、作用、用法
在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决 例如:@S ...
随机推荐
- css设置图片水平及垂直居中
.box { /*非IE的主流浏览器识别的垂直居中的方法*/ display: table-cell; vertical-align:middle; /*设置水平居中*/ text-align:cen ...
- Nchan 安装试用(openresty 同时支持)
备注: 使用nginx最新的源码包(nginx-1.13.6),以及源码安装 1. 下载源码包(nginx+ Nchan) https://nginx.org/download/ng ...
- 【学步者日记】C#使用线程
http://note.youdao.com/noteshare?id=2810300cdfa3f4d973792dcf30a31db9 System.Threading.Thread th; th ...
- JavaScript快速切换繁体中文和简体中文的方法及网站支持简繁体切换的绝招
一般商业网站都有一个语言的需求,就是为了照顾使用正体中文的国人,会特地提供一个切换到正体中文的选项(或曰“繁体中文”).传统做法是在服务端完成的,即通过某些控件或者过滤器转换文本语言.这里笔者介绍一种 ...
- ColorPic 一套簡單好用的顏色選擇器!
做美工的時候,常常會有配色.抓取顏色及獲取顏色代碼的困擾,專業人士可能有很好的工具來協助,但對於偶爾需要或非經常接觸美工的人來說,即便是有很好的工具,也還要花很多時間進行學習,常常就是看到一個漂亮的顏 ...
- APP推送通知相关实现
关于推送通知,iOS推送主要是通过服务端来实现的,相关过程可以参考下面两篇文章: http://cshbbrain.iteye.com/blog/1859810 http://zxs198 ...
- maven环境的搭建,lemon-OA办公系统的搭建
当时要搭建activiti工作流,但是这个工作流是基于maven启动的,于是,学习了一下,maven环境的搭建 准备的环境: Jdk 1.6 Eclipse IDE 一个或者 MyEclipse M ...
- C++ cosnt的一点总结
1,C++在定义函数重载的时候形参不管是不是const的他们都是等价的,除非形参是const引用.举个例子: void fun(int a){...}与void fun(const int a){.. ...
- Go - 切片(Slice)
定义 切片本身不是数组,它指向底层的数组或者数组的一部分.因此,可以使用Slice来处理变长数组的应用场景. Silice 是一种引用类型. 1.定义一个空的Slice package main im ...
- pubmed检索完全攻略
第一章 进入PubMed魔法学校--PubMed 概述 有位退休的老教授不止一次的向我感叹:"你们现在真是幸福,我们那时候要查一篇相关的文献,要到图书馆一本一本目录去检索.尤其是做一些别人不 ...