Tuesday, February 27, 2007

Generics Puzzler

I just read this Java generics puzzler from Pure Danger Tech. I have felt exactly this pain many times before. The decision to erase types from compiled bytecode causes so much pain. I don't think .NET uses type erasure, so I figured this would be pretty easy in C#:


static T[] merge<T>(params T[][] arrays)
{
int len = 0;
foreach (T[] array in arrays)
{
len += array.Length;
}
T[] result = new T[len];
len = 0;
foreach (T[] array in arrays)
{
foreach (T elem in array)
{
result[len++] = elem;
}
}
return result;
}

No comments: