Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my C# array lose type sign information when cast to object?

Investigating a bug, I discovered it was due to this weirdness in 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[]); 

The output is "True False True True", while I would have expected "bar is byte[]" to return False. Apparently bar is both a byte[] and an sbyte[]? The same happens for other signed/unsigned types like Int32[] vs UInt32[], but not for say Int32[] vs Int64[].

Can anyone explain this behavior? This is in .NET 3.5.

like image 836
Rngbus Avatar asked Jul 24 '09 17:07

Rngbus


People also ask

Why is my C drive so full?

▪You may have saved large files to C: drive that you are not aware of. For example, the default download folder is located in C: drive. ▪Pages files, previous Windows installation, temporary files, and other system files may have taken up the space of your system partition. There might be more reasons behind C: drive mysteriously full.

What are the possible causes of scanning and repairing drive C?

What are the possible causes of scanning and repairing drive c? The possible reasons that can hamper your computer and give you the message like "Scanning and repairing drive C" are given below: It happens when you have not shut your PC down properly be it because of power failure or forced shut down, etc.

Does your C drive keep filling up with space?

While browsing Windows forums, we find many users report their hard drive space disappearing. Although they don't store any more files on the drive, and even if they delete some files, their C drive keeps filling up. Here is a true example we extract from answers.microsoft.com:

What to do if the C drive cannot be accessed normally?

If the desktop cannot be accessed normally, there may be a problem with the Master Boot Record (MBR) of the C drive. At this point, you can use the following method to perform a test and repair. 1. Restart the computer and press “F8” before the Windows 11/10 logo appears.


1 Answers

UPDATE: I've used this question as the basis for a blog entry, here:

https://web.archive.org/web/20190203221115/https://blogs.msdn.microsoft.com/ericlippert/2009/09/24/why-is-covariance-of-value-typed-arrays-inconsistent/

See the blog comments for an extended discussion of this issue. Thanks for the great question!


You have stumbled across an interesting and unfortunate inconsistency between the CLI type system and the C# type system.

The CLI has the concept of "assignment compatibility". If a value x of known data type S is "assignment compatible" with a particular storage location y of known data type T, then you can store x in y. If not, then doing so is not verifiable code and the verifier will disallow it.

The CLI type system says, for instance, that subtypes of reference type are assignment compatible with supertypes of reference type. If you have a string, you can store it in a variable of type object, because both are reference types and string is a subtype of object. But the opposite is not true; supertypes are not assignment compatible with subtypes. You can't stick something only known to be object into a variable of type string without first casting it.

Basically "assignment compatible" means "it makes sense to stick these exact bits into this variable". The assignment from source value to target variable has to be "representation preserving". See my article on that for details:

http://ericlippert.com/2009/03/03/representation-and-identity/

One of the rules of the CLI is "if X is assignment compatible with Y, then X[] is assignment compatible with Y[]".

That is, arrays are covariant with respect to assignment compatibility. This is actually a broken kind of covariance; see my article on that for details.

https://web.archive.org/web/20190118054040/https://blogs.msdn.microsoft.com/ericlippert/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance/

That is NOT a rule of C#. C#'s array covariance rule is "if X is a reference type implicitly convertible to reference type Y, then X[] is implicitly convertible to Y[]". That is a subtly different rule, and hence your confusing situation.

In the CLI, uint and int are assignment compatible. But in C#, the conversion between int and uint is EXPLICIT, not IMPLICIT, and these are value types, not reference types. So in C#, it's not legal to convert an int[] to a uint[].

But it IS legal in the CLI. So now we are faced with a choice.

  1. Implement "is" so that when the compiler cannot determine the answer statically, it actually calls a method which checks all the C# rules for identity-preserving convertibility. This is slow, and 99.9% of the time matches what the CLR rules are. But we take the performance hit so as to be 100% compliant with the rules of C#.

  2. Implement "is" so that when the compiler cannot determine the answer statically, it does the incredibly fast CLR assignment compatibility check, and live with the fact that this says that a uint[] is an int[], even though that would not actually be legal in C#.

We chose the latter. It is unfortunate that C# and the CLI specifications disagree on this minor point but we are willing to live with the inconsistency.

like image 64
Eric Lippert Avatar answered Oct 13 '22 08:10

Eric Lippert