Steven Schveighoffer Wrote: > On Tue, 25 May 2010 02:26:02 -0400, Kagamin <s...@here.lot> wrote: > > > Recently I've hit a problem with collections in C#. Given classes > > class A {} > > class B {} > > And two collections Collection<A> and Collection<B> it's possible to > > concat them into an array A[]. The efficient way to do it is to use > > CopyTo(T[],int) method, but it accepts only array of exact collection > > item's type, so I had to change the Collection<B> type to Collection<A>. > > Did you mean > > class B : A {} ?
Ah, yes. > According to this page, it says that an exception could be thrown if "Type > T cannot be cast automatically to the type of the destination array." > This implies that a destination array type to which T could automatically > be cast should work. ICollection<A> acoll; ICollection<B> bcoll; A[] cat; ICollection<A>.CopyTo(A[],int) ICollection<B>.CopyTo(B[],int) - note the signature, the destination array can't be an array of supertype. There's no chance to throw an exception because the code doesn't pass type check at compile time.