Set class generic value in constructor only

.net, c#, generics

Solution

If I understand your requirements correctly, the easiest way to achieve this is to have a non-generic interface:

interface IGen { }

Then your generic class can be:

class Gen<T> : IGen { }

And the usage is:

IGen objectGen = new Gen<object>();
IGen intGent = new Gen<int>();

Problem

There is any way to set the generic type (T) of class in the constructor only, but not in the declaration? ``` public class Gen<T> { } public class Da { } public class Program { public static Gen<?> gen; public static void Main(string[] args) { gen = new Gen<Da>(); } } ```

Original source