Is a variant of this possible with C# Generics? -


am trying achieve impossible or unnecessary? code in getlist same specializations of class a, apart bit create new instance of specialization. there no default constructor, same list of parameters.

at moment using instead public static list<a> getlist(int i, func<a> createmeaninstance)

which feels unclean.

abstract class {   protected (int i)   {    }    public static list<t> getlist<t>(int i) t :   {     var list = new list<t>();     for(int x = 1; x< 100; x++)     {      var o = new t(i);      list.add(o);     }   }  }      public class b :  {     public b (int i) : base(i) {}  }   ...   var list = a.getlist<b>(1); 

you can't call parameterized constructors on generic types. func<> version best approach here.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -