奇怪:byte = byte + byte 报类型转换错???

GZ 2010-05-04 08:18:02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testbyte
{
class Program
{
static void Main(string[] args)
{
byte[] test1 = new byte[10];
byte[] test2 = new byte[10];

test1[0] = 0x30;
test2[0] = 0x22;

byte result = test1[0] + test2[0];
}
}
}


竟然有这样错误,why???
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
------ Build started: Project: testbyte, Configuration: Debug Any CPU ------
C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\testbyte.exe /target:exe Program.cs Properties\AssemblyInfo.cs
C:\Documents and Settings\z06189\My Documents\Visual Studio 2008\Projects\testbyte\testbyte\Program.cs(18,27): error CS0266: Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

Compile complete -- 1 errors, 0 warnings
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
...全文
95 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
GZ 2010-05-04
  • 打赏
  • 举报
回复
结果没超出范围
PeterSong9970 2010-05-04
  • 打赏
  • 举报
回复
结果超出范围了吧
GZ 2010-05-04
  • 打赏
  • 举报
回复
刚学习c#, c#确实是个有意思的语言
GZ 2010-05-04
  • 打赏
  • 举报
回复
应该是和被强制转换成int了,谢谢大家
testtestett 2010-05-04
  • 打赏
  • 举报
回复
char也是这种情况
lpingz 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gws044010607 的回复:]

难道说赋值到左边的时候,默认都是int?
[/Quote]

是byte和byte的和,的默认结果是int型的。
lpingz 2010-05-04
  • 打赏
  • 举报
回复
强制转换没问题

byte result = (byte)(test1[0] + test2[0]);
testtestett 2010-05-04
  • 打赏
  • 举报
回复
难道说赋值到左边的时候,默认都是int?
草帽精神 2010-05-04
  • 打赏
  • 举报
回复
学习了
testtestett 2010-05-04
  • 打赏
  • 举报
回复
是有点奇怪,int+int也有可能不在int的范围啊,为什么int能通过
upc_xiaowei 2010-05-04
  • 打赏
  • 举报
回复
byte在做运算的时候ms要转换成int
所有byte+byte的结果是int
lpingz 2010-05-04
  • 打赏
  • 举报
回复
哦 上面不对

报错原因是

赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。
lpingz 2010-05-04
  • 打赏
  • 举报
回复
结果不在0~255的范围了吧。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧