调查一个bug,我发现这是由c#中的这个奇怪的问题引起的:
sbyte[] foo = new sbyte[10];
object bar = foo;
Console.WriteLine("{0} {1} {2} {3}",
foo is sbyte[], foo is byte[], bar is sbyte[], bar is byte[]);
输出结果为“True False True True”,然而我本来期望"bar is byte[]
"返回False。显然,bar既是一个byte[]
又是一个sbyte[]
?其他有符号/无符号类型(如Int32[]
与UInt32[]
)也会出现相同的情况,但对于Int32[]
与Int64[]
这样的情况则不会出现。
有人能解释一下这种行为吗?这是在.NET 3.5中发生的。