java - How does C# do runtime generics? -
one thing irritates me java awful implementation of compile-time translation of generic type arguments.
i can observe , understand c# implementation far better, i'm confused how works.
essentially, how can say:
t t = new t() if don't know type of t , therefore don't know constructor argument requirements?
i can see
class<t> cl = t.class or
t[] tarr = new t[0] but don't see how can create new instance of t if don't know requirements of constructing it?
you can new t(); if t constrained have plain, parameterless public constructor, instance:
public class foo<t> t : new() { private myt = new t(); } additionally, there no way specify other sort of constructor exist. not possible:
// doesn't work public class foo<t> t : new(string, int) { private myt = new t("foo", 5); } to other points, how type of t @ runtime:
var ttype = typeof(t); and creating array of t doesn't create instances (unless t value type, in case creates default value of type):
// space 32 t's, nothing in array. // if t value type, int instance, // each 1 have default value (0 int, example) var arrayoft = new t[32];
Comments
Post a Comment