Is it possible in C# to create an array of unspecified generic types? Something along the lines of this:
ShaderParam<>[] params = new ShaderParam<>[5];
params[0] = new ShaderParam<float>();
Or is this simply not possible due to C#'s strong typing?
You could use a covariant interface for ShaderParam<T>
:
interface IShaderParam<out T> { ... }
class ShaderParam<T> : IShaderParam<T> { ... }
Usage:
IShaderParam<object>[] parameters = new IShaderParam<object>[5];
parameters[0] = new ShaderParam<string>(); // <- note the string!
But you can't use it with value types like float
in your example. Covariance is only valid with reference types (like string
in my example). You also can't use it if the type parameter appears in contravariant positions, e.g. as method parameters. Still, it might be good to know about this technique.
Rather late to the game, but here's how: http://www.codeproject.com/Articles/1097830/Down-the-Rabbit-Hole-with-Array-of-Generics
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With