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. Node.js入门笔记(2):全局对象(1)

    以下将以API文档为基础进行分析学习 global对象 这些对象在所有模块里都可用.有些对象不是在全局作用域而是在模块作用域里,这些情况下面文档都会标注出来. __filename--返回当前模块文件 ...

  2. [Head First设计模式]山西面馆中的设计模式——观察者模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 引言 不知不自觉又将设计模式融入生活了,吃个饭也不得安生,也发现生活中的很多场景,都可以用设计模式来模拟.原来设计模式就在 ...

  3. Swift 3.0 【Swift 3.0 相较于 Swift 2.2 的变化】

    一.编译器和语法变化 函数或方法参数 调用函数或方法时从第一个参数开始就必须指定参数名 在Swift的历史版本中出现过在调用函数时不需要指定任何函数参数(或者从第二个参数开始指定参数名),在调用方法时 ...

  4. 配置SQL server远程连接(局域网)

    具体步骤: 1) 2) 3) 4) 5) 6) 7) 最后为了防火墙有影响,直接把防火关了,测试连接通过在来设置防火墙.

  5. TCP/IP三次握手和HTTP过程

    1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在"无差别&qu ...

  6. python3简单爬虫

    最近在抽空学了一下python,于量就拿爬是练了下手,不得不说python的上手非常简单.在网上找了一下,大都是python2的帖子,于是随手写了个python3的.代码非常简单就不解释了,直接贴代码 ...

  7. CPU时间片

    CPU时间片 为了提高程序执行效率,大家在很多应用中都采用了多线程模式,这样可以将原来的序列化执行变为并行执行,任务的分解以及并行执行能够极大地提高程序的运行效率. 但这都是代码级别的表现,而硬件是如 ...

  8. FluentValidation

    git :https://github.com/JeremySkinner/FluentValidation Example using FluentValidation; public class ...

  9. ffmpeg-20160901-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 5 屏幕横向放大 20 像素 6 屏幕横向缩小 20 像素 S 下一帧 [ -2秒 ] +2 ...

  10. iOS应用文件夹

    IOS5多了一个比较重要的功能iCloud,但是同时也出现一个问题,很多的APP都把很大量的数据存在APP底下的Documents(/Documents )文件夹里面,这样苹果会reject掉你的AP ...