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>();
        .....
    }
}
Like The Article To View All

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...