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的更多相关文章

  1. Language and Compiler Features Since Delphi 7

    from: http://edn.embarcadero.com/cn/article/34324 Language and Compiler Features Since Delphi 7   In ...

  2. 自动重置Language level 5 与 Java Complier 1.5

    Intellij IDEA用Maven来构建项目,若pom.xml没有指定版本,总是默认Language level 5 与 Java Compiler 1.5. 以下是两种修改方式: 1. 手动进行 ...

  3. Maven 解决 下载项目 compiler 为1.5的问题

    在 开发Maven 项目的时候,会发现个问题,就是下载下来的项目默认 compiler 为1.5 ,项目报错. 明明之前开发用的是1.7的啊. 这里只需要在pom.xml确定下就好了. <pro ...

  4. ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)

    B - Benny's Compiler Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  5. 10 The Go Programming Language Specification go语言规范 重点

    The Go Programming Language Specification go语言规范 Version of May 9, 2018 Introduction 介绍 Notation 符号 ...

  6. Excellent JD

    Job description About the role We are looking for a talented engineer who has excellent cloud skills ...

  7. 【读书笔记】《Computer Organization and Design: The Hardware/Software Interface》(1)

    笔记前言: <Computer Organization and Design: The Hardware/Software Interface>,中文译名,<计算机组成与设计:硬件 ...

  8. 转 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 ...

  9. 堆栈 & Stack and Heap

    What's the difference between a stack and a heap? The differences between the stack and the heap can ...

随机推荐

  1. 关于Java集合的小抄

    在尽可能短的篇幅里,将所有List.Map.Set.Queue的特征与实现方式捋一遍.适合所有"精通Java"其实还不那么自信的人阅读. List ArrayList 以数组实现. ...

  2. ecshop 导出exl表格

    // 导出订单 if(isset($_POST['export'])){ // 统计金额 $sl = "SELECT SUM(goods_amount) as total from" ...

  3. ImageView的常用属性

    ImageView的一些常用属性,并且这些属性都有与之对应的getter.setter方法: android:adjustViewBounds:设置ImageView是否调整自己的边界来保持所显示图片 ...

  4. C#中Trim()、TrimStart()、TrimEnd()的用法

    string s = " from dual union all ";          s = s.Trim().TrimEnd("union all".To ...

  5. 如何自定义iphone个性铃音

    准备工作:itunes.(Netease Cloud Music).iphone 1.下载你想要的铃音原音乐: 2.打开itunes,向音乐库中添加刚刚下载的音乐: "文件"-&g ...

  6. codevs1540 银河英雄传说

    描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶集 ...

  7. 公众平台关注用户达到5万即可开通流量主功能 可以推广APP应用

    今天微信公众平台发布发布了一些更新,公众帐号的关注用户达到5万,即可开通流量主功能,之前的是要求10万粉丝,这是一个微信开放的信号.广告主可推广苹果商店应用或腾讯开放平台应用.新增卡片和图文广告规格. ...

  8. jQuery如何判断元素是否是隐藏的?

    jQuery函数简介: is(expr) 用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true. 如果没有元素符合,或者表达式无效,都返回'false'. 注 ...

  9. PHP变量入门教程(3)global 关键字

    global关键字 首先,一个使用 global 的例子: 使用 global <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b ...

  10. dos 操作显示 > nul 2>nul

    1>nul 屏蔽操作成功显示的信息,但是出错还是会显示(同 >nul)2>nul 屏蔽操作失败显示的信息,但是成功还是会显示>nul 2>nul 就是正确的错误的一起屏蔽 ...