Re: narrowing conversions with arrays

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: John Wood (spam_at_isannoying.com)
Date: 04/15/04


Date: Thu, 15 Apr 2004 21:00:19 GMT

ok slight confusion, sorry.
   Base[] bases = new Base[1];
   bases[0] = new Inherited();
   inheriteds = (Inherited[])bases;

Causes the runtime error. You changed it to:
   Base[] bases = new Inherited[1];
   bases[0] = new Inherited();
   inheriteds = (Inherited[])bases;

Which does work.

So the compiler doesn't raise an error because bases can contain an instance
of an array of any type derived from Base -- and the compiler doesn't know
which particular instance it contains.

Makes sense....

Learnt something else while playing with this... the following code produces
this exception:

An unhandled exception of type 'System.ArrayTypeMismatchException' occurred
Attempted to store an element of the incorrect type into the array.

  class Base { }
  class Base2 : Base { }
  class Inherited : Base { }

  Base[] bases;
  Inherited[] inheriteds;

  Base[] bases = new Inherited[1];
  bases[0] = new Base2();
  inheriteds = (Inherited[])bases;

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1ae9025c1d406b0598a6c1@msnews.microsoft.com...
> John Wood <spam@isannoying.com> wrote:
> > What ver of .net are you running?
> > When I run the code on 1.0 it causes a runtime error. Perhaps it's fixed
in
> > 1.1.
>
> It's certainly fine on 1.1, but I'm surprised it fails on 1.0. What
> error are you getting? Are you running the *exact* code I posted?
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Relevant Pages

  • problem using class librarys
    ... The problem I’ve got: runtime error: ... An unhandled exception of type 'System.Runtime.Remoting.RemotingException' occurred in mscorlib.dll ... Additional information: Remoting configuration failed with the exception System.IO.FileNotFoundException: File or assembly name System.Runtime.Remoting, or one of its dependencies, was not found. ... namespace BasicClient ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: segmentation fault on calloc
    ... > Failure to #include something is only going to confuse the compiler. ... returns `int', while in fact it returns `void *'. ... which may well cause a runtime error. ...
    (comp.lang.c)
  • Re: Why do i get runtime error ?
    ... I get runtime error for the above code. ... That depends on the compiler. ... mismatches between a printf format string and arguments (if the format ... Some compilers might be able to help you diagnose ...
    (comp.lang.c)
  • Re: matmul trouble
    ... I'm puzzled that this should turn into a runtime error with 123456789 as ... and direct form would count as "bonehead". ... This has zero to do with reading array input. ... Compiler installation problems, including such things as managing to ...
    (comp.lang.fortran)
  • Re: code error
    ... > declaration but nothing if somrbody can try this on their compiler to make ... > sure is not a compiler compatibility problem. ... You say you're getting a runtime error. ...
    (comp.lang.c)