I’ve made a function that processes an array of objects, process(Object[]). It works on any type including integers and floats so long as you box each element first. I’d like the function to take unboxed elements aswell, if only to box it itself before processing. How do I do that?
I could wrap with a few functions like process(int[]) and process(float[]) but that also seems a hassle. I’ve tried process(ValueType[]) but the compiler still selects process(Object[]).
I have C# 2.0, but if there is a nice solution for 3.0 I’d like to see it.
Short answer: you can’t (usefully) do this. Generics might seem to be a way around this, but as soon as you actually use the generically typed instance, you’ll be passing it to methods that must accept class object – so it’ll be boxed then (worse; it’ll be boxed many times probably, depending on your code).
If you want to gain performance by avoiding boxing generically, you’ll need to work on how you process these instances and ensure that all processing can occur in a type-safe manner despite the generic approach – and that’s not easy (see Generic Numerics for example) and depends heavily on the processing you need to do.