.Net is known for its array covariance. That means that any array can be cast to an array of base elements:
public class T: B { } T[] tlist = ... B[] blist = tlist;
This feature comes at cost:
B b = ... T t = ... blist[0] = b; // This efficiently is: blist[0] = (T)b; tlist[0] = t; // This is the same: tlist[0] = (T)t;
We pay the cost of additional cast, just for nothing. Let this dubious design decision opresses .Net/Java inventors.
You can eliminate the cast. Just use array of structs:
struct S<T> { public T t; } S<T>[] slist = ... slist[0].t = t; // Works without cast.
Measurment show that S[] is ~35% faster than T[] on write, and slower (JIT could do better) on read.
Well, ugly workaround of ugly design.
P.S. In java there is no relief...
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u