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 ...
随机推荐
- MySQL中地理位置数据扩展geometry的使用心得
最近学习了些MySQL geometry数据存储和计算,在这里记录下. 1. 环境 geometry推荐在5.6版本以上使用,尽管大部分功能在5.5已经可用,除了距离计算函数st_distance等新 ...
- Kanboard 看板工具配置使用
备注: 类似的开源工具有wekan 界面还有功能和Trello 类似.比较方便 1. 安装(基于docker+ docker-compose) a. 安装docker && ...
- MesureDeviceWebServiceDAS
package com.accu.business.pms.webservice; import java.net.URL; import javax.xml.namespace.QName;impo ...
- opencv读取中文路径报错的问题
) ## 经验证,不需要再转bgr,myImread的读图结果已经是和imread一样的 return img
- Unit07: MyBatis框架简介 、 MyBatis基本应用
Unit07: MyBatis框架简介 . MyBatis基本应用 1. myBatis (1)myBatis是什么? 是一个开源的持久层框架. 注:myBatis底层仍然是jdbc. (2)编程步骤 ...
- 使用maven打包额外的jar
当使用maven打包的时候,部分自己的额外使用的在maven基础库里面是没有的时候: 参考: https://blog.csdn.net/hguisu/article/details/5107268 ...
- DomHelper
public class DomHelper { public static ArrayList<Person> queryXML(Context context) { ArrayList ...
- MSYS2环境搭建
本机环境:Windows XP 32位MSYS2地址:http://sourceforge.net/projects/msys2/ 下载32位版本,地址:http://sourceforge.net/ ...
- MySQL COUNT(*) & COUNT(1) & COUNT(col) 比较分析
在面试的时候我们会经常遇到这个问题: MySQL 中,COUNT(*).COUNT(1).COUNT(col) 有区别吗? 有区别. 接下来我们分析一下这三者有什么样的区别. 一.SQL Syntax ...
- https 请求的端口是443 注意
注意: 这里录制https的请求 端口号一定是443 才可以抓取到!!!!!! (进坑多次)