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;

最新文章

  1. Android的RecyclerView
  2. JQuery AJAX 解析获得的JSON数据
  3. Linux:远程到linux的图形界面
  4. 初始化windows窗口
  5. 无法自动调试 未能调试远程过程。这通常说明未在服务器上启用调试 WCF 托管在IIS上
  6. Unicode与UTF-8互转(C语言实现)
  7. maven web项目不能创建src/main/java等文件夹的问题
  8. 排序算法入门之冒泡排序及其优化(java实现)
  9. MOCK API 的定义及实践(使用eolinker实现)
  10. Prometheus使用入门
  11. eclipse连接github,链接不上 cannot open git-upload-pack(git-receive-pack)
  12. 图片居中table-cell
  13. &lt;构建之法&gt;第11、12章
  14. 数据结构与算法Java描述 队列
  15. Go Example--range
  16. Java读取文件-BufferedReader/FileReader/InputStreamReader/FileInputStream的关系和区别
  17. C# delegate 委托
  18. PCL的PNG文件和计算点云重心
  19. AppScan扫描建议 问题集
  20. 【转】MFC 字体LOGFONT

热门文章

  1. 各种HTTP状态的含义
  2. SQL SERVER批量修改表名前缀
  3. 7 款顶级开源 BI(商务智能)软件和报表工具
  4. yaf设置命名空间
  5. 关于hg的命令
  6. linux下共享库的注意点之-fpic
  7. [Scala] 快学Scala A1L1
  8. Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解
  9. C# 类动态添加属性、方法(Z)
  10. win7无法保存打印机设置(错误0x000006d9)解决方法