c# - Why can I cast an A[] to a B[] if A and B are enum types? -
enum makes code more readable , easy understand in many case. can't understand when can use line below :
public enum { apple,orange,egg } public enum b { apple,orange,egg } public static void main() { a[] aa = (a[])(array) new b[100]; }
can give me source code sample can used type of enum array.
the clr has more generous casting rules c# has. apparently, clr allows convert between arrays of enums if underlying type has same size both types. in question linked case (sbyte[])(object)(byte[])
surprising.
this in ecma spec partition i.8.7 assignment compatibility.
underlying types – in cts enumerations alternate names existing types (§i.8.5.2), termed underlying type. except signature matching (§i.8.5.2)
iii.4.3 castclass says
if actual type (not verifier tracked type) of obj verifier-assignable-to type typetok cast succeeds
castclass
instruction c# compiler uses perform cast.
the fact, enum members same in example has nothing problem. note, no new object being created. cast of object reference.
Comments
Post a Comment