日期:2014-05-16  浏览次数:20831 次

linq的Aggregate的一个编译错误,请教.
我想用BitConverter把一个int转变成一个byte数组,然后把byte数组的内容逐字节打印出来,如下::

                int i = 20;
                byte[] barray = BitConverter.GetBytes(i);
                Console.WriteLine(barray.Aggregate((a,b)=>a+","+b));

结果第三行有两个编译错误:
error CS0029: Cannot implicitly convert type 'string' to 'byte'
error CS1662: Cannot convert lambda expression to delegate type 'System.Func<byte,byte,byte>' because some of the return types in the block are not implicitly convertible to the delegate return type

我的程序应该怎么改? 多谢.
------解决方案--------------------
Console.WriteLine(barray.Aggregate(string.Empty, (a, b) => string.IsNullOrEmpty(a) ? b.ToString() : a + "," + b));
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案--------------------
a是byte类型,","是字符串,两者不能直接+,要转换格式
------解决方案--------------------
类型要强转。。