为什么 C# 中的字节减法需要强制转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/927391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is a cast required for byte subtraction in C#?
提问by Billy
I have to following code in VS2008 .net 3.5 using WinForms:
我必须使用 WinForms 在 VS2008 .net 3.5 中遵循以下代码:
byte percent = 70;
byte zero = 0;
Bitmap copy = (Bitmap)image1.Clone();
...
Color oColor = copy.GetPixel(x, y);
byte oR = (byte)(oColor.R - percent < zero ? zero : oColor.R - percent);
When I leave the "(byte)
" off the last line of code, I get a compiler error saying it "Cannot implicitly convert type 'int
' to 'byte
'." If everything is of type byte
and byte
is an integer type... then why do I need to have the cast?
当我离开“ (byte)
”关的最后一行代码,我得到一个编译器错误说它“无法隐式转换类型‘ int
’到‘ byte
’。” 如果一切都是类型byte
并且byte
是整数类型......那我为什么需要强制转换?
采纳答案by Charlie Martin
Because subtraction is coercing up to an integer. As I recall, byte is an unsigned type in C#, so subtraction can take you out of the domain of bytes.
因为减法强制为整数。我记得,字节是 C# 中的无符号类型,所以减法可以带你离开字节域。
回答by Andrew Hare
This is because byte
subtraction returns an int
. Actually any binary arithmetic operations on bytes
will return an int
, so the cast is required.
这是因为byte
减法返回一个int
。实际上,任何二进制算术运算bytes
都会返回一个int
,因此需要进行强制转换。
回答by Timothy Carter
Arithmetic on bytesresults in an int value by default.
默认情况下,字节算术结果为 int 值。
回答by Adam Wright
Because arithmetic on bytes returns integers by default so of the two possible assignments the narrower type of zero (byte) is promoted to int (that of oColor.r - percent). Thus the type of the operation is an int. The compiler will not, without a cast, allow you to assign a wider type to a narrower type, because it's a lossy operation. Hence you get the error, unless you explicitly say "I know I'm losing some data, it's fine" with the cast.
由于字节算术默认返回整数,因此在两种可能的赋值中,较窄的零类型(字节)被提升为 int(oColor.r 的类型 - 百分比)。因此,操作的类型是 int。如果没有强制转换,编译器将不允许您将较宽的类型分配给较窄的类型,因为这是一种有损操作。因此,您会收到错误消息,除非您对演员明确说“我知道我正在丢失一些数据,这很好”。
回答by schnaader
That's because the result of a byte subtraction doesn't fit in a byte:
那是因为字节减法的结果不适合一个字节:
byte - byte = (0..255) - (0..255) = -255..255
回答by Alexander Kahoun
回答by PeterAllenWebb
Because arithmetic operations on sbyte, byte, ushort, and short are automatically converted to int. The most plausible reason for this is that such operations are likely to overflow or underflow.
因为对 sbyte、byte、ushort 和 short 的算术运算会自动转换为 int。最合理的原因是此类操作很可能上溢或下溢。
Thus, in your ternary operation, the final oColor.R - percent, actually results in an int, not a byte. So the return type of the operation is int.
因此,在您的三元运算中,最终的 oColor.R - 百分比实际上是一个整数,而不是一个字节。所以操作的返回类型是int。
回答by Licky Lindsay
FWIW, java also promotes bytes to int.
FWIW,java 也将字节提升为 int。
回答by Licky Lindsay
Try to run this C# code: object o = (byte)(1); o = (int)o; What do you expect? Now try :)
尝试运行此 C# 代码: object o = (byte)(1); o = (int)o; 你能指望什么?现在试试:)
I think this is right:
我认为这是对的:
Eric Lippert says, "I don't think of bytes as "numbers"; I think of them as patterns of bits that could be interpreted as numbers, or characters, or colors or whatever. If you're going to be doing math on them and treating them as numbers, then it makes sense to move the result into a data type that is more commonly interpreted as a number."
Eric Lippert 说:“我不认为字节是‘数字’;我认为它们是可以被解释为数字、字符、颜色或其他任何东西的位模式。如果你要在它们并将它们视为数字,然后将结果移动到更常被解释为数字的数据类型中是有意义的。”
Perhaps byte is closer to char than to int.
也许字节更接近于字符而不是整数。