June 17th, 2009, 4:13 pm
QuoteOriginally posted by: mhollanderif (initvalue is ICloneable) m_arr = (T) (initvalue as ICloneable).Clone(); else m_arr = initvalue;reflection is probably slow -- i would avoid doing it twice (ie is ICloneable and then cast to ICloneable) and i would also avoid doing it inside the loop.you can certainly do ICloneable xxx = initValue as ICloneable; if (xxx == null) // handle not cloneable case here else // handle cloneable case here.HOWEVER, there is another issue with your code that I'm not sure you've thought of. It might act a bit strange. Say that T is not cloneable but T has a subclass which is cloneable. Then you will get reference copying or cloning depending on the instance not the class. Is that what you intend?The code is not optimal, indeed. The Array class can hold anything in principle, but in practice its client classes nearly always use numeric types (and then 'double' is 95% of the cases that you need, although its nice to have 'float' and complex, for example).The cloneable test in the loop is bad In practice, most data is value type (e.g. using structs would solve it) but on the other hand I would like Array<T> to be bullet proof. The interesting class for numerics is Vector<T> so that maybe I make Array private only. I don't think I will need subclasses of T.Will look at TPL to see how it handles vectors of numeric data.
Last edited by
Cuchulainn on June 16th, 2009, 10:00 pm, edited 1 time in total.