如何在 C# 中减去一行上的字节
这确实很奇怪。谁能解释一下吗?
这段代码不起作用:
const byte ASC_OFFSET = 96;
string Upright = "firefly";
byte c7 = (byte)Upright[6] - ASC_OFFSET;
//Cannot implicitly convert type 'int' to 'byte'.
这段代码也不起作用:
const byte ASC_OFFSET = 96;
string Upright = "firefly";
byte c7 = (byte)Upright[6] - (byte)ASC_OFFSET;
//Cannot implicitly convert type 'int' to 'byte'.
然而,将减法放在单独的行上效果很好:
const byte ASC_OFFSET = 96;
string Upright = "firefly";
byte c7 = (byte)Upright[6];
c7 -= ASC_OFFSET;
如果必须的话,我不介意将语句放在单独的行上......但我想知道...
为什么?
This is really odd. Can anyone explain this?
This code does NOT work:
const byte ASC_OFFSET = 96;
string Upright = "firefly";
byte c7 = (byte)Upright[6] - ASC_OFFSET;
//Cannot implicitly convert type 'int' to 'byte'.
This code also does NOT work:
const byte ASC_OFFSET = 96;
string Upright = "firefly";
byte c7 = (byte)Upright[6] - (byte)ASC_OFFSET;
//Cannot implicitly convert type 'int' to 'byte'.
Yet, putting the subtraction on a separate line works just fine:
const byte ASC_OFFSET = 96;
string Upright = "firefly";
byte c7 = (byte)Upright[6];
c7 -= ASC_OFFSET;
I don't mind putting the statements on separate lines, if I have to... but I have to wonder...
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 1)
byte
运算结果为int
(请参阅此处的原因:http://blogs.msdn.com/b/oldnewthing/archive/2004/03/10/87247.aspx)和2)以下 C# 代码将在后台“神奇地”编译为
C# 规范中明确记录的内容: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
This is because 1)
byte
operations result inint
(see why here: http://blogs.msdn.com/b/oldnewthing/archive/2004/03/10/87247.aspx) and 2) the following C# codewill be "magically" compiled behind the scene into
This is explicitely documented in C# specification here: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
前两个示例无法编译的原因是:
字节上没有减法运算符,因为假设您有一个包含 7 的字节,并从中减去一个包含 8 的字节,您真的希望它成为字节 255 吗?我想大多数人会希望它是 int -1。
最后,你到底为什么要以字节为单位这样做?这没有任何意义。 C# 中的字符不是字节;如果你想对字符进行算术,那么为什么不从字符'y'中减去char 96,而不是进行有损且危险的字节转换?
The reason why your first two samples do not compile is because:
There is no subtraction operator on bytes because, well, suppose you have a byte containing 7 and you subtract from it a byte containing 8, do you really want that to be the byte 255? I think most people would want that to be the int -1.
Finally, why on earth are you doing this in bytes in the first place? This doesn't make any sense. Chars aren't bytes in C#; if you want to do arithmetic on chars then why not subtract the char 96 from the char 'y' instead of doing a lossy and dangerous conversion to byte?
我以前也注意到过这一点。我认为这是因为
-=
运算符是为字节类型预定义的,而在前一种情况下,您实际上是将int
放入byte
中>,这是不允许的。他们这样做的原因不一定有意义,但它符合规则,因为在前一种情况下,编译器在进行赋值时无法“查看”-
运算符。如果您确实需要在一行上进行减法,请不要说: 而是
说:
I've noticed this before too. I think it's because the
-=
operator is predefined for byte types, whereas in the former cases, you're really putting anint
inside abyte
, which isn't allowed. The reason they did this doesn't necessarily make sense, but it's consistent with the rules, because in the former cases, the compiler can't "peek" at the-
operator when doing the assignment.If you really need to subtract on one line, instead of saying:
Say: