an excellent capability of C# language and compiler
Sometimes you want to write code that works for different primitive types, and as C# doesn't support generic type constraints on primitive type hence you can't avoid writing more code, but you may still one way or another minimise the code you have to write and mysteriously end up having to write something like below,
public override bool GetValue<T>(int row, int col, out T val)
{
CacheBlockDouble cache;
int r, c;
GetCacheAndPoint(row, col, out cache, out r, out c);
var dval = cache.Data[r, c];
val = (T) (object) dval;
return dval != NoDataValue;
}
The main concern is on line 7, where a seemingly boxing and unboxing is happening on primitive type dval (which is of double type here) and this is done like this
And we know that T is determined at compile time and is also double, so we hope that the compiler is smart enough to eliminate the unnecessary boxing and interpret that line as below at run time.
val = dval;
It looks like a simple task as everything can be easily evaluated at compile time. But we need proof before we can be sure.
The most reliable way is to examine the IL, however the following code is sufficient.
using System; namespace boxing
{
class Program
{
public static T BoxingTest<T>(double v)
{
T result = (T)(object)v;
return result;
} public static double NonboxingTest(double v)
{
return v;
} static void Main(string[] args)
{
long countDown = ;
const int buffersize = ;
var buffer = new double[buffersize];
var t1 = DateTime.UtcNow;
var i = ;
for (; countDown>; countDown--)
{
var t = BoxingTest<double>(i);
//var t = NonboxingTest(countDown);
buffer[i] = t;
i++;
if (i == )
{
i = ;
}
}
var t2 = DateTime.UtcNow;
Console.WriteLine("finished in {0} secs", (t2-t1).TotalSeconds);
}
}
}
If we compare them, the generic method takes same amount of time as the non-generic counterpart for any iterations. To this point, I'm quite convinced.
And one more thing which is a bit disappointing kind of adds to the confidence: we can't do such thing like below, which will cause a run time exception InvalidCastException: Specifid cast is not valid
var d = 1.0;
var f = (float)(object)d;
Not even this,
var f = 1.0f;
var d = (double)(object)f;
an excellent capability of C# language and compiler的更多相关文章
- Language and Compiler Features Since Delphi 7
from: http://edn.embarcadero.com/cn/article/34324 Language and Compiler Features Since Delphi 7 In ...
- 自动重置Language level 5 与 Java Complier 1.5
Intellij IDEA用Maven来构建项目,若pom.xml没有指定版本,总是默认Language level 5 与 Java Compiler 1.5. 以下是两种修改方式: 1. 手动进行 ...
- Maven 解决 下载项目 compiler 为1.5的问题
在 开发Maven 项目的时候,会发现个问题,就是下载下来的项目默认 compiler 为1.5 ,项目报错. 明明之前开发用的是1.7的啊. 这里只需要在pom.xml确定下就好了. <pro ...
- ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)
B - Benny's Compiler Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 10 The Go Programming Language Specification go语言规范 重点
The Go Programming Language Specification go语言规范 Version of May 9, 2018 Introduction 介绍 Notation 符号 ...
- Excellent JD
Job description About the role We are looking for a talented engineer who has excellent cloud skills ...
- 【读书笔记】《Computer Organization and Design: The Hardware/Software Interface》(1)
笔记前言: <Computer Organization and Design: The Hardware/Software Interface>,中文译名,<计算机组成与设计:硬件 ...
- 转 A Week with Mozilla's Rust
转自http://relistan.com/a-week-with-mozilla-rust/ A Week with Mozilla's Rust I want another systems la ...
- 堆栈 & Stack and Heap
What's the difference between a stack and a heap? The differences between the stack and the heap can ...
随机推荐
- HTML5的属性
一.全局属性 1.class属性 class属性对元素指定CSS类选择器 <!doctype html> <html> <meta charset="utf-8 ...
- 【译文】Java Logging
本文讲Java内置的java.util.logging软件包中的 api.主要解释怎样使用该api添加logging到你的application中,怎样加配置它等.但是本文不谈你应该把什么东西写到日志 ...
- tyvj1203 机器分配
描述 总公司拥有高效生产设备M台,准备分给下属的N个公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M<=100,N& ...
- 如何设置phpMyAdmin自动登录和取消自动登录
如何设置phpMyAdmin自动登录? 首先在根目录找到config.sample.inc.php复制一份文件名改为config.inc.php(如果已经存在 config.inc.php 文件,则直 ...
- winform窗体弹出时,光标默认显示在指定的输入框内
private void Form1_Paint(object sender, PaintEventArgs e) { this.textBox1.SelectAll(); this.textBox1 ...
- 百度地图SDK
百度地图官方SDK文档 http://lbsyun.baidu.com/index.php?title=androidsdk 一.申请百度地图SDK 每一个app对应一个百度地图AK,百度地图 ...
- canvas案例——画时钟
基本思路,先画一个200半径的圆 ctx.arc(250,250,200,0,2*Math.PI); 然后再画时分刻度,可以先利用translate变化坐标到圆的中心点,然后再通过rotate旋转 / ...
- [Linux][PHP]安装swoole扩展
1.下载swoole 2.解压并配置 /usr/local/php/bin/phpize ./configure --enable-swoole-debug --enable-sockets --en ...
- .NET 泛型
泛型 泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的指定推迟到客户端代码声明并实例化该类或方法的时候.例如,通过 ...
- UC浏览器 分享到朋友圈和微信好友 分类: JavaScript 2015-04-28 14:45 615人阅读 评论(1) 收藏
用手机UC浏览器访问新浪微博,会注意到有这样的两个分享按钮: 在手机端浏览器里,点击分享按钮,就可以启动微信客户端并分享到微信.研究了下其源代码,存在这样的一个js:http://mjs.sinaim ...