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 ...
随机推荐
- POJ3045 Cow Acrobats
题意 Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join t ...
- phoenxi elixir 框架几个方便的命令
1. 已有命令 mix app.start # Starts all registered apps mix app.tree # Prints the application tree mix ar ...
- vue数据已渲染成 但还是报错 变量 undefined
问题:页面上的数据已渲染出来,但是控制台还是报错变量未undefined,主要是当页面加载完成后,数据并未加载完,所以会报次错误. 解决办法:在数据渲染的主节点(最外层的div)添加 v-if=“da ...
- VS2010单元测试入门实践教程
单元测试的重要性这里我就不多说了,以前大家一直使用NUnit来进行单元测试,其实早在Visual Studio 2005里面,微软就已经集成了一个叫Test的专门测试插件,经过几年的发展,这个工具现在 ...
- ASP.NET常用标准配置web.config
在我们的项目开发过程中,我们经常要配置wei.config文件,而大多数的时候配置差不多,下面的是一个简单的配置,其他的配置可以在这个基础上在添加 <?xml version="1.0 ...
- 如何查看你的 FastAdmin 服务器是否开启了 gzip br 压缩
如何查看你的 FastAdmin 服务器是否开启了 gzip 压缩 gzip br 压缩的好处不用多说了. 但是怎么方便的知道网站到底有没有开启或使用了 gzip 呢,其实只要在浏览器就可以看到. 按 ...
- oracle之 利用 controlfile trace文件重建控制文件
一. 11g RAC 重建控制文件 1. --"create controlfile"命令生成到追踪文件中:alter database backup controlfile to ...
- 数据库的备份与恢复(oracle 11g) (转)
一. 内容与步骤 (注意这里许多步骤需要同学们查资料,理解并消化后才能完成) 1.数据库创建 (1) 安装Oralce11g: (2) 创建至少两个以上用户: (3) 每个用户 ...
- python TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed]
TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed] sql="insert into auto_tr ...
- codechef January Lunchtime 2017简要题解
题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...