I want to create a generic function with signature like this : void funcName<T>() where T would be required to be an implementation of some particular interface I want. How to make such check? How to pass to a generic function class type that implements certan interface?
So I create some public interface IofMine {} and I try to create a function like public static void funcName<T>() where T : IofMine { var a = new T} and sadly I get:
Error: Cannot create an instance of the variable type ‘T’ because it
does not have the new() constraint
What shall I do to make class types my function receives not only be of my desired interface but also have a constructor?
In order to require that the generic parameter have a default constructor, specify
new()as part of the generic constraint.You can only use this to require a default (i.e., no parameters) constructor. You can’t require a constructor taking a string parameter, for example.