Skip to content

Recursive Type Inheritance

Recursive type inheritance should be avoided.

What does this mean ?

In techniques when you can break out of it, recursion is permissible. When you use class types, though, you'll get code that compiles but doesn't run when you try to instantiate the class.

What can happen ?

If you try to instantiate the class, you'll get code that compiles but doesn't run.

Recommendation

When it comes to type inheritance, recursion should be avoided.

Sample Code

Vulnerable :

class C1<T>
{
}
class C2<T> : C1<C2<C2<T>>> // Noncompliant
{
}

...
var c2 = new C2<int>();

References