在 C# 5.0 in a Nutshell 的 C# 主题的Conversions(第 2 章)中,作者说:
...Conversions can be either implicit or explicit: implicit conversions happen automatically, and explicit conversions require a cast. In the following example, we implicitly convert an int to long type (which has twice the bitwise capacity of an int)...
这是例子:
int x = 12345; // int is a 32-bit integer
long y = x; // Implicit conversion to 64-bit integer
short z = (short)x; // Explicit conversion to 16-bit integer
按位容量和位容量有关系吗?或者,作者对按位容量的看法是什么?
请您参考如下方法:
我想,他想区分“按位容量”和“数字容量”。
在示例中,数据类型的位容量不同:int 有 32 位,long 64 位和 short 16 位。在这种情况下,转换为具有较高容量的数据类型是隐式的,转换为具有较低位容量的数据类型是显式的。
另一方面,有一些类似于“数字容量”的东西,其中 int 和 uint 确实共享相同的位数(它们具有相同的“按位容量”),但在您可以存储的值方面仍然不完全兼容(uint 不支持负值)。
