I have a class with an indexer:
public class SpecialArray<T>
{
public T this[long index]
{
get { return SpecialGetFunction(index) }
set { SpecialSetFunction(index, value); }
}
private void SpecialSetFunction(long index, T value) { ...blah blah... }
private T SpecialGetFunction(long index) { ...blah blah... }
}
I have another class that currently has an array in it:
public class BaseClass<T>
{
private T[] _actual_nodes;
public virtual T[] Nodes
{
get { return _actual_nodes; }
set { _actual_nodes = value; }
}
}
Now I want to override BaseClass<T>
so it will use my indexed class, rather than the _actual_nodes
array.
public class SomeDerivedClass<T> : BaseClass<T>
{
private SpecialArray<T> _new_nodes_array;
public override T[] Nodes
{
get { return _new_nodes_array; }
set { _new_nodes_array = value; }
}
}
Unfortunately, this last class generates a compiler error, stating it cannot implicitly convert from type SpecialArray<T>
to T[]
(and vice versa)
So I’m just not sure how to make the get/set statement use my indexed class, rather than an array. At first I was wondering if there is there anyway to reference the index passed to the get/set for "Nodes"? But, even if there is, using it would still only allow me to get/set a single T element, not an array of T[]
.
QUESTION:
Anyone have thoughts on how can I make the overridden T[] Nodes{}
accessor use my indexed class to get/set values?
More academically, WHY doesn't an indexer, returning/accepting a variable of type X, IMPLY a conversion to an array of type X? Can such a conversion be explicitly defined somehow?
Constraint: I cannot make changes to the BaseClass
.