Generic type constraint of new() and an abstract base class
abstract-class, base-class, c#, generics
Solution
Alas you have to explicitly give the type
DoSomething<Derived>(d2);
theoretically is not possible to create something that is abstract
Problem
Here we have a simple class herarchy, and use of generics with a type constraint of `new()` ``` public abstract class Base { } public class Derived : Base { } public class TestClass { private void DoSomething<T>(T arg) where T : new() { } public void TestMethod() { Derived d1 = new Derived(); DoSomething(d1); // compiles Base d2 = new Derived(); DoSomething(d2); // compile error } } ``` The code fails to compile at the indicated line, with an error of: 'Base' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Foo.DoSomething(T)' This error is clear and makes sense, but I had hoped that the compiler would understand that all derivations of `Base` (that could be instantiated at this point) do have a public parameterless constructor. Would this be theoretically possible for the compiler?