I'm trying to understand the exact difference between the 3 following methods of the ImmutableArray struct:
- the static method ImmutableArray<T>.CastUp<TDerived>
- the instance method ImmutableArray<T>.As<TOther>
- the instance method ImmutableArray<T>.CastArray<TOther>
I found myself studying these methods because I have the need to convert an existing instance of ImmutableArray<TDerived>
to an instance of ImmutableArray<TBase>
.
My scenario is similar to the following:
public interface IFoo {}
public sealed class Foo: IFoo {}
ImmutableArray<Foo> fooClassArray = GetFoos();
ImmutableArray<IFoo> fooInterfaceArray = // convert from fooClassArray instance
Based on my understanding, the way to do so efficiently is by using the ImmutableArray<T>.CastUp<TDerived> static method. So, I could solve my problem this way:
ImmutableArray<IFoo> fooInterfaceArray = ImmutableArray<IFoo>.CastUp(fooClassArray);
If I get it correctly, by doing so there is no need to reallocate the underlying array: the array being wrapped by fooClassArray
is reused and there is no need to allocate a brand new array for fooInterfaceArray
.
First question: is my understanding about ImmutableArray<T>.CastUp<TDerived> correct ?
By looking through the documentation for the ImmutableArray<T> struct I spotted the ImmutableArray<T>.As<TOther> method and the ImmutableArray<T>.CastArray<TOther> method.
What is the difference between them ?
What is the intended usage of them ? It seems to me that they both solve the same use case.
Are they used to perform downcasts operations (as opposed to ImmutableArray<T>.CastUp<TDerived>, which is actually used to perform upcast operations) ?
CastUp
doesn't copy the array, as you said. It is very efficient to use, as long as each element is trivially castable to a base type. Note that boxing or converting don't count as trivially casting (ie trying to use that to changeint
tolong
will fail):CastArray
seems to simply cast the entire array to the type you ask. It can work with covariance, so it should be more or less equivalent to the first function (again, boxing doesn't count). Be careful of the parameter type however, it is not anImmutableArray
:As
is the generalization ofCastUp
, allowing you to also cast down, at a cost: