C#int字节转换 [英] C# int byte conversion

查看:157
本文介绍了C#int字节转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

byte someVar;
someVar -= 3; 

有效,但

byte someVar;
someVar = someVar - 3;

不是吗?

推荐答案

令人惊讶的是,当你对字节执行操作时,计算将使用 int 值完成,字节隐式转换为(int )首先。这对于也是如此,同样 float s被上转换为执行浮点运算时双倍

Surprisingly, when you perform operations on bytes the computations will be done using int values, with the bytes implicitly cast to (int) first. This is true for shorts as well, and similarly floats are up-converted to double when doing floating-point arithmetic.

第二个代码段相当于:

byte someVar;
someVar = (int) someVar - 3;

因此你必须将结果强制转换回(byte)让编译器接受赋值。

Because of this you must cast the result back to (byte) to get the compiler to accept the assignment.

someVar = (byte) (someVar - 3);

这篇关于C#int字节转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆