sbyte[] 可以神奇地转换为 byte[]

sbyte[] can be magically cast to byte[]

我不确定这是否是一个 .NET 错误,但我发现它真的很有趣。

不出所料,我做不到:

sbyte[] sbytes = { 1, 2, 3 };
byte[] bytes = sbytes; // fails: cannot convert source type 'sbyte[]' to taget type 'byte[]'

但是,如果 sbytes 的类型是 object,这有效:

object obj = new sbyte[]{ 1, 2, 3 };
byte[] bytes = obj as byte[];
Assert.IsNull(bytes, "WTF??")

备注 1int[] - uint[] 和其他原始类型也会出现同样的问题。

备注 2:虽然代码将数组作为 byte[] 处理,但调试器会失去焦点并在数组中显示 ?-s。

备注 3:这仅适用于数组,不适用于底层类型本身:

object sbyteObj = (sbyte)1;
byte byteObj = (byte)sbyteObj; // System.InvalidCastException: Specified cast is not valid.

好的,我当然可以这样检查类型:

if (obj.GetType() == typeof(byte[]))

这是 as 运算符和直接转换的预期行为,还是 .NET 错误?

不,这不是错误。这只是 C# 语言规则(声称没有可用的转换)和 CLR 规则(其中转换 可用)之间的阻抗不匹配。

请注意,编译器真的,真的认为它最了解:

byte[] bytes = new byte[10];
// error CS0030: Cannot convert type 'byte[]' to 'sbyte[]'
sbyte[] sbytes = (sbyte[]) bytes; 

即使您的代码在编译时出现警告,它也不会真正按照它说的去做:

byte[] bytes = new byte[10];
// warning CS0184: The given expression is never of the provided ('sbyte[]')
if (bytes is sbyte[])
{
    Console.WriteLine("Yes");
}

运行 那个代码和你 没有 得到输出...但是如果你只是改变 bytes 的编译时类型,它是否 打印是:

object bytes = new byte[10];
// No warning now
if (bytes is sbyte[])
{
    Console.WriteLine("Yes"); // This is reached
}