2

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;
    }
}
}
Cœur
  • 34,719
  • 24
  • 185
  • 251
A191919
  • 3,342
  • 7
  • 37
  • 85

2 Answers2

4

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));
Matthew Watson
  • 96,889
  • 9
  • 144
  • 240
0

No, you can only do that with types (T) that have a constructor which takes no arguments. From MSDN:

The type argument must have a public parameterless constructor.

rory.ap
  • 32,474
  • 10
  • 77
  • 158