I see... yes, the non-intuitive thing about arrays in .NET, that I tend to forget, is 
that you can't cast an Object[] to a Foo[], even if every single element in the 
Object[] happens to be an instance of Foo.

IOW, the following code throws InvalidCastException:

  object[] foos = new object[3];

  foos[0] = new Foo();
  foos[1] = new Foo();
  foos[2] = new Foo();

  Foo[] bar = (Foo[])foos; // throws!

Thanks for the clarity, Joroen.
-S


On Fri, 10 Jan 2003 14:07:51 +0100, Jeroen Frijters <[EMAIL PROTECTED]> wrote:

>As long as the cast succeeds performance should be the same, if the cast
>fails, isinst is much faster than castclass (because castclass has to throw
>an exception, which is expensive).
>
>Note that neither of these instructions look at the elements of the array,
>i.e. only the type of the array is considered, not its contents.
>
>Regards,
>Jeroen
>
>> -----Original Message-----
>> From: Moderated discussion of advanced .NET topics.
>> [mailto:[EMAIL PROTECTED]] On Behalf Of
>> Shawn A. Van Ness
>> Sent: Thursday, January 09, 2003 22:29
>> To: [EMAIL PROTECTED]
>> Subject: [ADVANCED-DOTNET] Performance: castclass vs. isinst?
>>
>>
>> Which is faster?  For arrays, do both castclass and isinst
>> have to walk the list of elements, checking each one?
>>
>> I find I never know when to write
>>
>>     string[] foo =
>>     (string[])arraylist.ToArray(typeof(string)); // castclass
>> vs
>>     string[] foo =
>>     arraylist.ToArray(typeof(string)) as string[]; // isinst
>>
>> The latter is easer to type (fewer parens ;-) but seems to
>> give fits to VS's intellisense and auto-formatting features.  :-(
>>
>> Cheers,
>> -Shawn
>> http://www.arithex.com/
>>
>> You can read messages from the Advanced DOTNET archive,
>> unsubscribe from Advanced DOTNET, or
>> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>>
>
>You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
>DOTNET, or
>subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to