I’ve got two classes:
public abstract class Uniform<T>
public class UniformMatrix4 : Uniform<Matrix4>
(So far….there will be more that implement different types)
And now lets say I want to write a function that will accept any uniform object… but I can’t do that because there is no class called Uniform, only the generic Uniform<T>. So what’s the best approach to solving this problem?
- Make
Uniform<T>implementIUniform - Make
Uniform<T>extendUniform - Make all my functions that accept a Uniform to be generic too so that they can take a
Uniform<T>directly?
Make your methods generic as well, and you’re good.
Note that you always have the choice of using all generic type arguments on the function if needed, like this:
The compiler will usually infer the type arguments on his own whenever you have a parameter, so that the call in fact looks like a normal method invocation in the C# source code: