Consider the following classes; Getter and Caster where Getter retrieves a value T. Caster is a wrapper for Getter that casts the retrieved value and is a Getter itself.
public abstract class Getter<T>
{
public abstract T Get();
}
public class Caster<TIn, TOut> : Getter<TOut>
{
private Getter<TIn> getter;
public Caster(Getter<TIn> getter)
{
this.getter = getter;
}
public TOut Get()
{
return (TOut)(object)getter.Get();
}
}
The caster uses boxing creates garbage every time Get is called. Is there any way to cache the box and change the value or something to avoid the garbage? Maybe there's a better solution than boxing?