Search code examples
c#genericstype-constraints

Pass parameter through constructor in generic constraints


I want to pass parameter in constructor into generic function with constraints. Is it possible to create instance of T with parameters? Something like list.Add(new T(1));

namespace ConsoleApplication
{
class Program
{
    static void Main(string[] args)
    {
        List<Base> list = new List<Base>();
        AddElement<Quick>(list,5);
        AddElement<Range>(list, 5); 
        Console.WriteLine(list.Count);
        Console.ReadKey();
    }
    public static void AddElement<T>(List<Base> list, int number) where T : Base, new ()
    {
       for (int i = 0; i < number; i++)
       {
           //do not working
           list.Add(new T(i));
       }
    }
}
public abstract class Base
{

}

public class Quick:Base
{
    private int ID;
    public Quick()
    {

    }
    public Quick(int ID)
    {
        this.ID = ID;
    }
}

public class Range : Base
{
    private int ID;
    public Range()
    {

    }
    public Range(int ID)
    {
        this.ID = ID;
    }
}
}

Solution

  • The usual way around this is to pass a factory method or Func<T> to the method:

    public static void AddElement<T>(List<Base> list, int number, Func<T> factory) where T : Base
    {
        for (int i = 0; i < number; i++)
        {
            list.Add(factory());
        }
    }
    

    You can use that with your Quick class like this:

    var baseList = new List<Base>();
    AddElement(baseList, 5, () => new Quick(1));
    

    If you wanted to be able to pass the int constructor argument inside AddElement<T>() you can use a Func<int, T> instead like so:

    public static void AddElement<T>(List<Base> list, int number, Func<int, T> factory) where T : Base
    {
        for (int i = 0; i < number; i++)
        {
            list.Add(factory(i));
        }
    }
    

    Then call it like so:

    var baseList = new List<Base>();
    AddElement(baseList, 5, x => new Quick(x));