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) ?