castclass allows arrays of enum types to be cast to an array of the
underlying type (and v.v.), this is similar to the way the verifier
treats these arrays. However, for signed vs unsigned arrays the verifier
treats them as equivalent, whereas castclass doesn't allow the cast.
This seems inconsistent.

I've been thinking some more, and here are some disturbing example of
counterintuitive behaviour I came up with:

Code:
 public static void Func1(int[] ai)
 {
  // according to the C# spec these two statements should be equivalent:
  foreach(object o in ai) Console.WriteLine(o);
  for(IEnumerator e = ai.GetEnumerator(); e.MoveNext();)
Console.WriteLine(e.Current);
 }
Output:
 0
 Zero

Code:
 public static void Func2(int[] ai)
 {
  int[] c = (int[])ai.Clone();
 }
Output:
 System.InvalidCastException: Specified cast is not valid.
   at Test.Func2(Int32[] ai)
   at Test.Main()

Code:
 public static void Func3(int[] ai)
 {
  foreach(int i in ai) Console.Write(i + ", ");
  Console.WriteLine();
  Array.Sort(ai);
  foreach(int i in ai) Console.Write(i + ", ");
  Console.WriteLine();
 }
Output:
 3, -3, 2, -2, 1, -1, 0,
 0, 1, 2, 3, -3, -2, -1,

Now, to be clear, I don't mind this behaviour, but I do think people
should be aware of it. One way to do that is to make the behaviour
consistent across castclass (& isinst) and reflection (and to document
it). I'm pretty sure 99.99% of all C# programmer would not have expected
the above results to be possible (from verifiable code).

Regards,
Jeroen

> -----Original Message-----
> From: Discussion of the Rotor Shared Source CLI
> implementation [mailto:[EMAIL PROTECTED]] On
> Behalf Of Peter Drayton
> Sent: Tuesday, August 20, 2002 02:51
> To: [EMAIL PROTECTED]
> Subject: Re: [DOTNET-ROTOR] sbyte[] == byte[]
>
>
> > And of course, this is also begs the question why castclass
> > doesn't work for casting a byte[] to an sbyte[].
>
> Not quite sure what you mean here. castclass is an IL opcode,
> byte/sbyte
> are C# types. Are you objecting to the behaviour of the C#
> compiler, or
> the castclass opcode? [1]
>
> --Peter
> http://www.razorsoft.net/weblog
> http://staff.develop.com/peterd
>
> [1] I'm sure you're aware of this distinction, I'm just trying to get
> clarity on your question.
>

Reply via email to