【发布时间】:2011-02-27 14:19:00
【问题描述】:
代码说明:
int i = 5;
object obj = i;
byte b = (byte)obj; // X
运行时,这会在“X”行生成 System.InvalidCastException(“指定的强制转换无效”)。做双重演员工作:
byte b = (byte)(int)obj;
我原以为您应该能够将装箱的 int(如果它的值在 0..255 范围内)转换为字节。任何人都可以对此有所了解吗?
(这是在 .net 2.0 中,以防万一)。
【问题讨论】: