All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET数据库编程快速入门之技术慨述_[Asp.Net教程]
用户体验:JS实现仿新浪信息提示效果_JavaScript技术_编程技术
防止刷新网页,自动跳回到页面顶部的办法_[Asp.Net教程]
让PHP支持页面回退的两种方法_php资料_编程技术
打造Ajax简单相册_[AJAX教程]
ASP.NET技巧:GridView控件自定义分页详解_.net资料_编程技术
巧用Asp.net代码在虚拟主机实现Urlrewrite_[Asp.Net教程]
修改Zend引擎实现PHP源码加密的原理及实践_php资料_编程技术
VC#2005快速入门之使用if语句_[Asp.Net教程]
asp.net点击textbox自动清除其中文字_[Asp.Net教程]
Reading number is top 10 pictures
福利福利。。。。。。
Photographed the passion of the clients and prostitutes in the sex trade picture2
联通的3G无线网卡我只用了一天,看看流量......
YangYuYing and ZhengShaoQiu dance on the generous come interest dye-in-the-wood
The real super beauty2
Sora aoi on twitter4
乳娘帕梅拉安德森1
30 beautiful school beauty3
美女当网吧管理员的悲剧
Compared GDP and per capita income in China for 40 years
Download software ranking
Sora aoi's film--cangkong_Blue.Sky
Tram sex maniac 2 (H) rar bag2
Unix video tutorial1
Ashlynn Video4
打鸟视频
好色的外科大夫
Prostitutes diary
WebService在.NET中的实战应用教学视频 → 第4集
I'm come from Beijing2
linux高级编程
aaa published in(发表于) 2013/12/18 7:57:46 Edit(编辑)
C#中ArrayList.CopyTo

C#中ArrayList.CopyTo

C#中ArrayList.CopyTo()运行错误的解决方法_.net资料_编程技术-你的首页-uuhomepage.com

  在开始讲解C#中ArrayList.CopyTo()运行错误的解决方法之前,先给大家看一段代码:


ArrayList list = new ArrayList();
  list.Add(1);
  list.Add(2);
  byte[] buf = new byte[2];
  list.CopyTo(buf);


  这段代码看上去,好正常,编译也很顺利通过,不过执行的时候出错了,为什么?


  解剖ArrayList,其内部是用一个object 数组实现的,作为一切元素的容器,值类型必须转换成引用类型来储存,不然就不需要2.0的Generic出现了。 而CopyTo函数内部是用Array.Copy()来实现的,问题就出在它身上。当我们添加常量值到list里面,例如 list.Add(1),这里的1被看成是int,box储存在object后再unbox回int,于是就变成高精度向低精度复制了 int[]->byte[],就出错了,相当于如下代码:


  int[] a1 = new int[2]{1,2};
  byte[] a2 = new byte[2];
  Array.Copy( a1, a2, 2 );


  回过头来,原来的代码要修改也很简单,只要box之前把它变成小精度类型就可以了,修改后的代码如下:


  ArrayList list = new ArrayList();
  list.Add((byte)1);
  list.Add((byte)2);
  byte[] buf = new byte[2];
  list.CopyTo(buf);





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.