A191919
A191919

Reputation: 3442

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;
    }
}
}

Upvotes: 2

Views: 98

Answers (2)

Matthew Watson
Matthew Watson

Reputation: 109547

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));

Upvotes: 4

rory.ap
rory.ap

Reputation: 35260

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.

Upvotes: 0

Related Questions