I just observed a weird phenomenon in C#/.NET.
I created this minimal example to demonstrate:
if (new sbyte[5] is byte[])
{
throw new ApplicationException("Impossible!");
}
object o = new sbyte[5];
if (o is byte[])
{
throw new ApplicationException("Why???");
}
This will throw “Why???”, but not “Impossible!”. It works for all arrays of integral types of the same size. Can someone explain this to me? I’m confused. I’m using .NET 4 by the way.
P.S.: I know that I can get the expected result by using o.GetType() == typeof(byte[]).
The CLR rules of casting specify that this is possible. The C# rules say it is not possible. The C# team consciously decided that they would tolerate this deviation from the spec for various reasons.
Why does the CLR allow this? Probably because they can conveniently implement it.
byteandsbytehave the same binary representation so you can “treat” abyte[]as ansbyte[]without violating memory safety.The same trick works for other primitive types with the same memory layout.