It's not clear why you're converting to float?
at all (or why you're using ulong
as the index variable type...) but you just need to cast the result back to T
- otherwise you can't assign it back into an array of type T[]
. Additionally, you need to cast to object
(in order to convert back to T
:
float x = ((float) (object) data[i]) / sum;
data[i] = (T) (object) x;
You can use float?
for the first line, with as
, to avoid boxing - but then you need to get the non-nullable value:
float x = (data[i] as float?).Value / sum;
Both are pretty ugly :(
As noted in comments though, this sort of thing is usually an indication of the design not really being properly generic at all. We don't know what type Sum()
returns, but you should consider just how "general" your type is to start with.