You can add a restriction : new():
void Foo<T>() where T : class, new() {
T newT = new T();
}
If you don't have a limit, then it Activator.CreateInstance<T>can help (minus compile-time checking):
void Foo<T>() {
T newT = Activator.CreateInstance<T>();
}
If you mean yourself of the type itself, then probably something like:
Type itemType = typeof(int);
IList list = (IList)Activator.CreateInstance(
typeof(List<>).MakeGenericType(itemType));
source
share