Method One

Add a where T: new() after a class that uses generics

public class Example<T> where T : new()
{
    public static T Get()
    {
        T t = new T();
        ...........
    }
}

Method Two

You can use System.Activator.CreateInstance<T>() to create generic instance pairs

public class Example<T>
{
    public static T Get()
    {
        T t = System.Activator.CreateInstance<T>();
        .....
    }
}

All Comments

Leave a Reply Cancel Reply

Tips: Your email address will not be disclosed!

If you can't see clearly,please click to change...

Popular Posts

Collections