In C#, how to instantiate a passed generic type inside a method?

Declare your method like this:

public string InstantiateType<T>(string firstName, string lastName) 
              where T : IPerson, new()

Notice the additional constraint at the end. Then create a new instance in the method body:

T obj = new T();    

Leave a Comment