
Signature
C#, .NET and Complex Adaptive Systems:
> The underlying implementation of CopyTo() in NameObjectCollectionBase is
> private, so I can't call down to it. There's no implementation in the
> underlying base class to handle this.
The implementation is not visible if you have a variable of type
NameObjectCollectionBase, but NameObjectCollectionBase implements
ICollection. Simply cast your reference of NameObjectCollectionBase to
ICollection and you'll have a base CopyTo implementation that you can use to
implement your strongly-typed CopyTo.
I.e. (warning: air code, extra work may be required)
classs MyNameCollection : NameObjectCollectionBase
{
// this should shuttup FXCop!:
public void CopyTo(MyObjectType[] array, int index)
{
((ICollection)this).CopyTo(array, index);
}
Now you just let CopyTo deal with the specifics of the copying. Since the
array reference is passed in by value, there's no way to "swap out" the
original array that's being passed in, so you shouldn't worry about whether
the target array is large enough. If it isn't, CopyTo should throw an
ArgumentException. It's the responsibility of the caller to make sure the
array is large enough.
If you want a CopyTo-style method that does 'grow' the array as needed, you
should create a different method with this signature (or have the user pass
in an ArrayList):
public void InflatableCopyTo(ref MyObjectType[] array, int index)
{
// code to grow array here.
}
Since you can't 'grow' an array, you have to re-allocate and swap out the
old one for the new one, you'll need a ref or out on the parameter to tell
the caller you will (potentially) be changing the array.
HTH,
Richard
> The keys and values are collections
> (not arrays) that also don't support CopyTo(). So how do I write a
[quoted text clipped - 56 lines]
> > >
> > > Pete Davis