Why does my C# array lose type sign information when cast to object? -
Checking a bug, I found out that this strangeness caused C #:
< Code> sbyte [] Foo = new sbyte [10]; Object bar = foo; Console.light line ("{0} {1} {2} {3}", Foo is sbyte [], Foo byte [], bar is sbyte [], bar is byte []);
The output is "True Falls True True", while I expected that " bar byte []
" to return false. Apparently both have a byte []
and a sbyte []
? For such other signed / unsigned type, such as int32 []
versus UInt32 []
, but to say int32 []
vs Int64 []
.
Can anyone explain this behavior? It is in .NET 3.5. Update: I've used this question as the basis of a blog entry, here:
See blog comments for an extended discussion of this issue. Thanks for the great question!
You stumbled across an interesting and unfortunate dissonance between the CLI type system and the C # type system.
CLI "Assignment Compatibility" If the value of the known data type S is X "Assignment Compatible", then with a special storage location, the type of known data type, you can store y in y. If not, doing so does not have the verification code and the verifier will reject it.
For example, the CLI type system subtypes of reference type are compatible with the supertypes of the reference type. If you have a string, you can store it in the object's variable, because both are reference types and a subtype of the string object. But the opposite is not true; Supertypes are not compatible with sub-types, you can not stick to it, only known as an object in some type of string without the first string.
Actually "assignment compatible" means "it makes sense to paste these variable bits into this variable". Assignment should be the "representation protection" from the source value to the target variable, for details see my article on it:
One of the rules of CLI "If X assigns Y Compatible with, [X] Assignment is compatible with Y [] ".
That is, in relation to the assignment compatibility, it is actually a type of sovereign; See my article on it for details.
This is not the rule of C #. Array Sovereign Rule of C # "If X is a reference type which is inherently variable for reference type Y, then [X] it is vaginally variable []". This is a completely different rule, and so is your misleading situation.
In CLI, UIIT and int assignment are consistent. But in C #, the conversion between int and UIT is visual, not impile, and these are the value types, not reference types, in C #, it is not legal to convert it to a valid [UIT] .
But this is legal in CLI. So now we have to face an alternative.
1) The implementation is "so" so that when the compiler can not determine the answer, it actually calls a method that checks all C # rules for identity-protection convertibility and it is slow and 99.9 % Time matches CLR rules. But we hit the performance so that 100% compliance with the rules of C #.
2) Apply "is" so that when the compiler can not set the answer steadily, then this incredibly fast CLR assigns compatibility check, and stay with the fact that it says That a UIT [] is an int, however, it will not actually be legal in C #.
We chose later, it is unfortunate that the C # and CLI specifications disagree at this minor point but we are ready to live with incompatibility.
Comments
Post a Comment